Posted on: Nov 05, '08

how to check the empty directory in shell script
How to check the empty directory in shell script? This can be easily done with the help
ls command.
we can simply check the empty directory using following script.
DIR="/path/to/directory/"
if [ "$(ls -A $DIR)" ]; then
echo "Take action $DIR is not Empty"
else
echo "$DIR is Empty"
You can also assign the output of specific command to a separate variable instead of checking it directly in if condition.
e.g.
myVar=`(grep 'stringToSearch' filename.ext)`;
Note: Do not use spaces before and after =.
Tags: php, shell, shell script, linux