Bash: Check if file is found and contains data

The following code will check if the file described by variable FILENAME exists and has data.

if [ ! -f "$FILENAME" ]; then

    echo "Error: '$FILENAME' file not found.";

    if [ ! -s "$FILENAME" ]; then

        echo "Error: '$FILENAME' file is empty.";

        echo "Info: '$FILENAME' file found and contains data.";

This post is also available in: Greek

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.