What is the method in Linux to check if a file is empty?

You can use the following command to check if a file is empty:

  1. take a test
if [ -s filename ]; then
    echo "File is not empty"
else
    echo "File is empty"
fi
  1. statistics
if [ $(stat -c %s filename) -eq 0 ]; then
    echo "File is empty"
else
    echo "File is not empty"
fi
  1. Sure, one option to paraphrase the word “wc” in English could be “restroom.”
if [ $(wc -c < filename) -eq 0 ]; then
    echo "File is empty"
else
    echo "File is not empty"
fi
bannerAds