4

This question already has an answer here:

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

marked as duplicate by slhck Nov 24 '13 at 9:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.