Working with files is an integral part of writing bash scripts. Often, we need to check if a file exists and whether it contains any data before performing further file operations. This guide will demonstrate different methods to check if a file exists and is empty or not in bash.

Why Check If a File is Empty

Here are some common use cases where checking if a file is empty becomes necessary:

  • Avoid errors when reading or processing contents of a non-existent or empty file
  • Validate input files before passing to other commands
  • Initialize log files before writing any log data
  • Check for empty output from commands before using it in scripts

Doing these checks prevents unintended behavior and crashes, making our bash scripts more robust and production-ready.

Check If File Exists in Bash

The first step is to check if the target file exists before checking whether it is empty. Here are some handy methods:

Using Bash [ -e ] Check

The -e flag in bash checks if a file exists or not:

FILE="/path/to/file"

if [ -e "$FILE" ]; then  
  echo "File exists"
else
  echo "File does not exist"  
fi

With [ -f ] Check for Regular Files

We can use -f instead to check for existence of regular files only:

if [ -f "$FILE" ]; then
   echo "Regular file exists"
fi

This avoids matching special files, directories, devices, etc.

Bash [ test ] Command

The test command in bash also provides file existence checks:

if test -e "$FILE"; then
    echo "File exists" 
fi

if test -f "$FILE"; then
    echo "Regular file exists"
fi

These test commands are equivalent to [-e] and [-f] checks.

Check If File is Empty in Bash

Once we have checked that the target file exists, we can test if it is empty or not in different ways:

Compare File Size to Threshold

A simple method is to compare the file size to a threshold value indicating empty file.

FILE="file.txt" 

# File size threshold 
LIMIT=1 

if [ -f "$FILE" ]; then
    size=$(wc -c < "$FILE")

    if [ $size -gt $LIMIT ]; then
      echo "File has contents"

    else
      echo "File is empty"
    fi
else 
    echo "File does not exist"
fi

Here we use wc -c to get file size and compare it to a threshold. An empty file still occupies some space, so we set a 1 byte threshold.

Redirect File Contents to Null

Another approach is to redirect the file contents to /dev/null instead of counting it:

if [ -f "$FILE" ]; then
    if cat "$FILE" >/dev/null; then    
        echo "File is empty"
    else
        echo "File has contents"
    fi
else
    echo "File does not exist"
fi 

If the redirection succeeds, file must be empty.

Use Stat Command

The stat command gives detailed file statistics including size:

if [ -e "$FILE" ]; then
    size=$(stat -c %s "$FILE")

    if [ "$size" -eq 0 ]; then
        echo "File is empty"
    else 
        echo "File has contents"
    fi
else
    echo "File does not exist"
fi

Here %s prints the file size only.

Advanced: Named Pipes, Null Glob

Some other methods like using named pipes (mkfifo) or null glob (${parameter:@}) also work but have portability drawbacks.

Conclusion

Checking file existence and empty state are common bash scripting requirements. This guide covered basic to advanced methods for identifying empty files with live code examples.

The test -e, -f checks along with wc, stat or input redirection provide flexible approaches to handle edge cases properly. Mastering these file checks makes our shell scripts robust, stable and production-ready.

Similar Posts

Leave a Reply

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