bash check if a folder is empty or not

To check if a folder is empty of not, you can use

if [ "$(ls -A /home/boby/1/ )" ]
then
    echo "Files found"
else
    echo 'No files found'
fi

If you want to reverse the checking, you can use !

if [ ! "$(ls -A /home/boby/1/ )" ]
then
    echo "No files found"
else
    echo 'Files found.'
fi

See bash

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *