So far I've got this. Is there a more efficient way?
if ls -1 $HOME/path/to/folder/* >/dev/null 2>&1
then
echo Directory is not empty.
else
echo Directory is empty.
fi
So far I've got this. Is there a more efficient way?
if ls -1 $HOME/path/to/folder/* >/dev/null 2>&1
then
echo Directory is not empty.
else
echo Directory is empty.
fi
This is a standard exercise in all books/blogs/manuals about shells: in bash
[ "$(ls -A /path/to/directory)" ] && echo "Not Empty" || echo "Empty"
Bash does not have a standard test for a directory being empty, like [ -f file ], so we have to write it. The test && then 1 || or 2 is a standard way to condense a conditional statement of the form
if condition is satistfied
then do 1
else
do 2
fi