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
| Test | Checks | Returns True If | Usage |
|---|---|---|---|
| -s | File size | File exists AND size > 0 | [ -s "$file" ] |
| ! -s | File size | File empty or doesn’t exist | [ ! -s "$file" ] |
| -z | String | String is empty | [ -z "$var" ] |
| -n | String | String is not empty | [ -n "$var" ] |
| -f && -s | File + size | File 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
| Operator | Meaning |
|---|---|
-s file | File exists and has size > 0 |
! -s file | File is empty or doesn’t exist |
-f file -a ! -s file | File 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
- Not quoting variables -
[ -z $var ]fails if var has spaces - Using = instead of -z -
-zis correct test - Not checking file exists -
-sreturns false for non-existent files - Whitespace-only files -
-sdoesn’t detect these
Key Points
- Use
-sfor file size test - Use
-zfor 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.