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.

1
2
3
4
5
6
7
8
9
10
11
12
13
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: Αγγλικα

Απάντηση

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