Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to see if a directory exists on a remote server?

Perhaps there's a better way, but I'm writing an application deployment script, and I want to create a directory on a remote server if the directory doesn't exist to place the files.

Thanks in advance!

share|improve this question

You can use ssh to call a programme on the remote host, test tests for certain conditions.

if [[ `ssh test -d /path/to/my/directory && echo exists` ]] ; then
    # put code in here
share|improve this answer
This didn't work for me (probably your backquotes got mangled). I used if [[ `ssh test -d /path/to/my/directory && echo exists` ]] and that worked. – chiborg May 28 '10 at 8:31

Just use

ssh remoteHost 'mkdir -p /whatever/your/dir/is'

It will create the dir if it doesn't already exist.

share|improve this answer
if you add a -p to your mkdir command it will not report an error if the directory already exists and it will make the entire path. – Zoredache Jan 15 '10 at 21:02
I added it, but it depends on what exactly he wants. Now he can't use the command in an if statement anymore. – Kim Jan 16 '10 at 7:08
mkdir -p

But if that isn't quite what you're after you can check the existence of a directory with

ssh user@host test -d /home && echo exists
share|improve this answer

I'd recommend looking at using the RPM mechanism to install your application, rather than writing something home grown, since the problems you'll come across with your own script will almost certainly have already been solved with RPM. Here's an excellent tutorial on RPM.

share|improve this answer

How do you intend to talk to this remote server? SSH, SMB, NFS, something else? there's usually a way of finding this stuff out but you'll need to know what protocols and authentication option you have open to you before knowing exactly how to to this.

share|improve this answer
It can be accessed through SSH, however I wasn't sure if there is another tool that will allow me to create the directory. – user31899 Jan 15 '10 at 19:49
If you have SSH simply script an mkdir, if it exists it'll just fail with an error. – Chopper3 Jan 15 '10 at 20:01
Yeah, after doing some research, this is the way I'll be doing it. I'm kind of a linux noob, so I didn't realize you could execute a script and have it continue to execute commands remotely. – user31899 Jan 15 '10 at 20:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.