In shell script,
how to check whether a directory is non-empty on a remote machine?
Regards
In shell script,
how to check whether a directory is non-empty on a remote machine?
Regards
find PATH_TO_REMOTE_DIRECTORY -maxdepth 0 -not -empty
-maxdepth 0
will make sure you only check the directory, and not all subdirectories recursively.
-not -empty
is straightforward.
This will print the path if not empty, and will print nothing if empty.
And, of course, run it via ssh
.
One possibility is to execute the command mentioned in this answer via ssh
:
if [ "$(ssh user@host ls -A /dir/ 2>/dev/null)" == "" ]; then echo "empty"; else echo "not empty"; fi
Note: non-existing directories will also be reported as empty.