Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory exists on remote machine with ssh

Tags:

bash

ssh

This question has been already posted but I would like to know if there is a way to know if a directory exists on a remote machine with ssh BUT from the command line directly and not from a script. As I saw in this previous post: How to check if dir exists over ssh and return results to host machine, I tried to write in the command line the following:

ssh [email protected] '[ -d Documents ]'

But this does not print anything. I would like to know if there's a way to display an answer easily.

like image 900
hackchoc Avatar asked Nov 30 '15 21:11

hackchoc


2 Answers

This is a one liner:

ssh [email protected] '[ -d Documents ] && echo exists || echo does not exist'
like image 106
Jahid Avatar answered Nov 06 '22 04:11

Jahid


That command will just return whether or not the Documents directory exists, you can extend the answer in the linked question to do something if it does like:

if ssh [email protected] '[ -d Documents ]'; then
    printf "There is a Documents directory\n"
else
    printf "It does not exist, or I failed to check at all\n"
fi

or if you want to store whether or not it exists in a variable you could do something like

ssh [email protected] '[ -d Documents ]'
is_a_directory=$?

now if is_a_directory contains 0 you know there is a Documents directory, otherwise there is not such a directory or we failed to ssh and find out

like image 4
Eric Renouf Avatar answered Nov 06 '22 04:11

Eric Renouf