Checking for Empty Files in Bash

Checking for Empty Files in Bash

Bash scripting often requires verifying file existence and content. A common task is determining if a file is empty before proceeding with further operations. This tutorial explains how to reliably check for empty files using Bash commands and conditional statements.

Understanding File Size and Existence

Before diving into the commands, it’s important to understand the difference between a file that doesn’t exist and an empty file. A file that doesn’t exist simply isn’t present on the filesystem. An empty file does exist, but contains no data – its size is zero bytes.

The -s Operator

The most straightforward way to check if a file is empty (or has a size greater than zero) is using the -s operator within a conditional statement ([ ] or [[ ]]).

if [ -s filename ]; then
  # File exists and has content (size > 0)
  echo "File is not empty"
else
  # File does not exist OR is empty (size is 0)
  echo "File is empty or does not exist"
fi

The -s operator returns true if the file exists and has a size greater than zero. Otherwise, it returns false.

Example:

touch my_file.txt  # Create an empty file
if [ -s my_file.txt ]; then
  echo "File is not empty"
else
  echo "File is empty"
fi
# Output: File is empty

echo "Adding some content..." > my_file.txt
if [ -s my_file.txt ]; then
  echo "File is not empty"
else
  echo "File is empty"
fi
# Output: File is not empty

Handling Non-Existent Files

The -s operator doesn’t distinguish between an empty file and a file that doesn’t exist. If you need to specifically check for a file’s existence before checking if it’s empty, use the -f operator.

if [ -f filename ]; then
  # File exists
  if [ -s filename ]; then
    # File exists and is not empty
    echo "File exists and has content"
  else
    # File exists but is empty
    echo "File exists but is empty"
  fi
else
  # File does not exist
  echo "File does not exist"
fi

The -f operator returns true if the file exists and is a regular file (not a directory, symbolic link, etc.). This provides a more robust check, preventing errors when the file doesn’t exist.

Checking for Empty Files with Only Whitespace

Sometimes, you might need to check if a file is effectively empty even if it contains only whitespace characters (spaces, tabs, newlines). In this case, you can use grep in combination with a conditional statement:

filename="my_file.txt"
if [[ -z $(grep '[^[:space:]]' "$filename") ]]; then
  echo "File is empty or contains only whitespace"
else
  echo "File contains non-whitespace characters"
fi

This command uses grep to search for any character that is not whitespace ([^[:space:]]). If grep doesn’t find any such characters, its output will be empty. The [[ -z ... ]] condition checks if the output of grep is empty.

Concise Check with Logical Operators

For a quick check, you can combine the -s operator with the || (OR) operator:

[ -s filename ] || echo "File is empty or does not exist"

This is a shorthand way of writing:

if [ ! -s filename ]; then
  echo "File is empty or does not exist"
fi

Checking Multiple Files

To check multiple files, you can use a for loop:

for file in *.txt; do
  if [ ! -s "$file" ]; then
    echo "$file is empty"
  fi
done

This loop iterates through all .txt files in the current directory and prints the names of any empty files.

Leave a Reply

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