Skip to main content

How to Check if File is Empty

• 2 min read
bash

Quick Answer: Check if File is Empty

To check if a file is empty in Bash, use the -s operator: if [ -s "$file" ] returns true if file has size > 0. Use -z to check if a string is empty: if [ -z "$string" ].

Quick Comparison: Empty Testing Methods

TestChecksReturns True IfUsage
-sFile sizeFile exists AND size > 0[ -s "$file" ]
! -sFile sizeFile empty or doesn’t exist[ ! -s "$file" ]
-zStringString is empty[ -z "$var" ]
-nStringString is not empty[ -n "$var" ]
-f && -sFile + sizeFile AND has content[ -f "$file" -a -s "$file" ]

Bottom line: Use -s for files, -z for strings.


Test if files and strings are empty in Bash. Learn using -z, -s, and file size operators.

Check if File is Empty

# Test if file is empty (size 0)
if [ -s "$file" ]; then
  echo "File has content"
else
  echo "File is empty"
fi

# Or using negation
if [ ! -s "$file" ]; then
  echo "File is empty"
fi

File Size Operators

OperatorMeaning
-s fileFile exists and has size > 0
! -s fileFile is empty or doesn’t exist
-f file -a ! -s fileFile exists and is empty

Practical Example

#!/bin/bash

file="$1"

if [ ! -f "$file" ]; then
  echo "ERROR: File not found"
  exit 1
fi

if [ -s "$file" ]; then
  echo "✓ File has content"
  lines=$(wc -l < "$file")
  echo "  Lines: $lines"
else
  echo "✗ File is empty"
fi

Check if String is Empty

# Test if string is empty
if [ -z "$string" ]; then
  echo "String is empty"
fi

# Or not empty
if [ -n "$string" ]; then
  echo "String has content"
fi

Process Non-Empty Files

#!/bin/bash

for file in /var/log/*.log; do
  if [ -s "$file" ]; then
    echo "Processing: $file"
    # Process file
  else
    echo "Skipping empty: $file"
  fi
done

Remove Empty Files

#!/bin/bash

# Find and remove empty files
find . -type f -size 0 -delete

# With confirmation
find . -type f -size 0 -exec rm -v {} \;

Check Multiple Files

#!/bin/bash

files=("file1.txt" "file2.txt" "file3.txt")

for f in "${files[@]}"; do
  if [ -s "$f" ]; then
    echo "$f: OK ($(wc -c < $f) bytes)"
  else
    echo "$f: EMPTY"
  fi
done

Count Non-Empty Files

#!/bin/bash

directory="$1"
count=0

for file in "$directory"/*; do
  if [ -f "$file" ] && [ -s "$file" ]; then
    ((count++))
  fi
done

echo "Non-empty files: $count"

Check Line Content (Not Just Size)

#!/bin/bash

file="$1"

# File with only whitespace
if [ -f "$file" ]; then
  if [ -s "$file" ] && [ -n "$(tr -d ' \t\n' < "$file")" ]; then
    echo "File has non-whitespace content"
  else
    echo "File is empty or whitespace-only"
  fi
fi

Common Mistakes

  1. Not quoting variables - [ -z $var ] fails if var has spaces
  2. Using = instead of -z - -z is correct test
  3. Not checking file exists - -s returns false for non-existent files
  4. Whitespace-only files - -s doesn’t detect these

Key Points

  • Use -s for file size test
  • Use -z for empty string
  • Always quote variables
  • Check file exists first
  • Handle whitespace-only content

Summary

Testing for empty files and strings is essential. Use -s for files, -z for strings, and always quote variables properly.