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.";
else

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

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

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

This post is also available in: Greek

Leave a Reply

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