How to check a non-empty directory on a remote mac

2019-07-19 09:00发布

In shell script,

how to check whether a directory is non-empty on a remote machine?

Regards

标签: linux shell
2条回答
Emotional °昔
2楼-- · 2019-07-19 09:23

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.

查看更多
ら.Afraid
3楼-- · 2019-07-19 09:41

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.

查看更多
登录 后发表回答