0% found this document useful (0 votes)
11 views2 pages

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

The document describes how to check if a file is empty or not using shell scripting in Linux/UNIX. It provides examples of using the -s option of the test builtin and writing a shell script to check file emptiness.

Uploaded by

Probal Sil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

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

The document describes how to check if a file is empty or not using shell scripting in Linux/UNIX. It provides examples of using the -s option of the test builtin and writing a shell script to check file emptiness.

Uploaded by

Probal Sil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format [Link]

biz/ ~ RSS Feed ~ Facebook ~ Twitter ~ Google+ ~ Youtube


Copyrighted material

Home > FAQ > Linux

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


Posted By nixCraft <webmaster@[Link]> On July 3, 2009 @ 1:09 pm [ 4 Comments ]

How do I check if file is empty or not using bash or ksh shell script under UNIX / Linux / OS X / BSD operating systems? You can use the find command 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. 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.
[1]

Shell -s option
However, use can pass -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 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"
nixCraft is GIT UL++++ W+++ C++++ M+ e+++ dPage 1 of 2

[ $# -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 [Link] ./[Link] /etc/[Link] Sample outputs: /etc/[Link] has some data. Run it on an empty file: touch /tmp/[Link] ./[Link] /tmp/[Link] Sample outputs: [Link] is empty.

Important Message from nixCraft: 4000+ howtos and counting! Want to read more Linux / UNIX howtos, tips and tricks? We request you to sign up for the following to ensure that you make the most from our guides / howtos: 1. RSS feed for nixCraft - Get intimated about our new howtos / faqs as soon as it is released. 2. Daily email newsletter or weekly newsletter - Get intimated about our new howtos / faqs as soon as it is released via email.

URL to article: [Link] URLs in this post: [1] Image: [Link]


Copyrighted material Copyright 2006-2013 nixCraft. All rights reserved. This print / pdf version is for personal non-commercial use only. Unless otherwise indicated, the documents and graphics stored on this Web server, [Link], are copyrighted. Links to these documents are permitted and encouraged. No copies may be made without permission. More details - [Link]

nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-

Page 2 of 2

You might also like