5

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
slhck
  • 235,242

1 Answers1

9

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
MariusMatutiae
  • 48,517
  • 12
  • 86
  • 136