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

Need help with Linux Server or WordPress? We can help!

Leave a Reply

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