Check to see if a directory exists remotely (shell script)
Solution 1
You can use ssh to call a programme on the remote host, test
tests for certain conditions.
if [[ `ssh [email protected] test -d /path/to/my/directory && echo exists` ]] ; then
# put code in here
fi
Solution 2
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
Solution 3
Just use
ssh remoteHost 'mkdir -p /whatever/your/dir/is'
It will create the dir if it doesn't already exist.
Solution 4
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.
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on September 17, 2022Comments
-
Admin almost 2 years
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!
-
ILIV over 14 yearsIt can be accessed through SSH, however I wasn't sure if there is another tool that will allow me to create the directory.
-
Chopper3 over 14 yearsIf you have SSH simply script an mkdir, if it exists it'll just fail with an error.
-
ILIV over 14 yearsYeah, 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.
-
Zoredache over 14 yearsif 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. -
Ashish Ashu over 14 yearsI added it, but it depends on what exactly he wants. Now he can't use the command in an if statement anymore.
-
chiborg about 14 yearsThis didn't work for me (probably your backquotes got mangled). I used
if [[ `ssh [email protected] test -d /path/to/my/directory && echo exists` ]]
and that worked.