Check if directory exists on remote machine with ssh

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


This is a one liner:

ssh [email protected] '[ -d Documents ] && echo exists || echo does not exist'

Tags:

Bash

Ssh