Linux / UNIX: Check If File Is Empty Or Not Using Shell Script nixCraft Updated Tutorials/Posts

How do I check if a file is empty or not using bash or ksh shell script under a UNIX / Linux / macOS / OS X / BSD family of operating systems? How do I check if a file is empty in Bash?

You can use the find command and other options as follows. The -s option to the test builtin check to see if FILE exists and has a size greater than zero. It returns true and false values to indicate that file is empty or has some data. This page shows how to check if a file is empty in Bash shell running on a Linux or Unix-like operating systems.

Check If File Is Empty Or Not Using Shell Script

The syntax is as follows:

touch /tmp/file1
ls -l /tmp/file1
find /tmp -empty -name file1

Sample outputs:

/tmp/file1

Now create another file with some data in it:

echo "data" > /tmp/file2
ls -l /tmp/file2
find /tmp -empty -name file2

You should not see any output from the find command.

Bash Script – Check If File is Empty or Not With the -s Option

However, one can pass the -s option as follows in script or shell prompt:

touch /tmp/f1
echo "data" >/tmp/f2
ls -l /tmp/f{1,2}
[ -s /tmp/f1 ] echo $?

Sample outputs:

1

The non zero output indicate that file is empty.

[ -s /tmp/f2 ]
echo $?

Sample outputs:

0
Bash Shell Check If File Is Empty Or Not on a Linux/Unix
Bash Script/Command – Check If File is Empty or Not

The zero output indicate that file is not empty. So you can write a shell script as follows.

Shell Script To Check If File Is Empty OR Not

#!/bin/bash
_file="$1"
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
[ ! -f "$_file" ] && { echo "Error: $0 file not found."; exit 2; } if [ -s "$_file" ] then echo "$_file has some data." # do something as file has data
else echo "$_file is empty." # do something as file is empty 
fi

Run it as follows:
chmod +x script.sh
./script.sh /etc/resolv.conf

Sample outputs:

/etc/resolv.conf has some data.

Run it on an empty file:
touch /tmp/test.txt
./script.sh /tmp/test.txt

Sample outputs:

test.txt is empty.

Conclusion

This page showed how to check if file is empty on a Linux or Unix-like operating systems using various commands. See test command for more info.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments

More Stuff

How To Find My Public IP Address From Command Line On a Linux nixC... How do I find out my public IP address on the Linux and OS X Unix command line to use with my own bash shell script without using third party web sit...
Debian/Ubuntu: Setup Planet Venus To Combine Two Or More RSS Feeds... How do I setup “Planet Venus” under Debian or Ubuntu Linux to downloads news feeds (RSS) published by web sites and aggregates their content together...
Ansible guide, Raspberry Pi, Node.js, Python, the Linux desktop, a... For more discussion on open source and the role of the CIO in the enterprise, join us at The EnterprisersProject.com. The opinions expressed on this w...
PHP/MySQL Tutorial By Graeme Merral : Open source has brought a lot more than Linux to the computing world. It has also given us PHP and MySQL. According to Graeme, PHP ...
Spread the love

Posted by News Monkey