Linux / UNIX: Check If File Is Empty Or Not Using Shell Script

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:


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:


The non zero output indicate that file is empty.

[ -s /tmp/f2 ]
echo $?

Sample outputs:


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

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

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

Sample outputs:

/etc/resolv.conf has some data.

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

Sample outputs:

test.txt is empty.


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.

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Ankit Jul 14, 2011 @ 13:13

    Given shell script is not executing the then block of the if condition………….it always executing else block, either input file is empty or not…………..

  • raj Mar 14, 2012 @ 16:57

    Very helpful …

  • Vivan Apr 27, 2012 @ 12:56

    What if you there might be multiple files exist? ex:

    file1, file2, file3

    if [ -s “$file*” ] or if [-s file*]
    do not work
    any suggestions?


  • 🐧 nixCraft Jul 9, 2012 @ 20:14


    You need to edit the script:


  • gof Sep 26, 2012 @ 12:44

    How do send emails on the sample outputs in linux as a crontab

  • Kevin Sep 4, 2016 @ 21:03

    This was very helpful, I have it in a script to email me when there are failed login attempts on my servers!

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @