LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-01-2006, 07:39 AM   #1
spiffytech
Member
 
Registered: Dec 2005
Location: NC, USA
Distribution: (K)ubuntu
Posts: 139

Rep: Reputation: 15
How can I make my script check to see if a file is empty?


I need to make my script check to see if a text file is empty so I can fork the process depending on whether or not it is. How do I do that?
 
Old 01-01-2006, 08:20 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Something like this...
Code:
#!/bin/bash
# Example: ./test file.txt

fsize=$(stat -c %s $1)

if [ $fsize -lt 1 ] ; then
   echo $1 is empty
   else
   echo $1 is not empty
fi

Last edited by homey; 01-01-2006 at 08:39 AM.
 
Old 01-01-2006, 08:31 AM   #3
LiNuXkOlOnIe
Member
 
Registered: Dec 2005
Location: North germany
Distribution: Linux Mint
Posts: 45

Rep: Reputation: 16
Quote:
Originally Posted by homey
Something like this...
Code:
#!/bin/bash
# Example: ./test file.txt

fsize=`stat -c %s $1`

if [ $fsize -lt 1 ] ; then
   echo $1 is empty
   else
   echo $1 is not empty
fi
The new way of command-substitution and recommended is:
fsize = $(stat -c %s $1)

The result of both is the same. Not an advise only how i make it cos the backtick could be overseen in an hot programming session.

Happy new year.
 
Old 01-01-2006, 08:38 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Thanks for the tip LiNuXkOlOnIe !
 
Old 01-01-2006, 09:08 AM   #5
jcliburn
Member
 
Registered: Dec 2003
Location: Mississippi, USA
Distribution: Fedora
Posts: 435

Rep: Reputation: 33
Bash has a built-in test for this and many other file attributes (see the bash manpage). The "-s" option tests for a file with non-zero size.

Code:
#!/bin/bash

if [ -s $1 ]; then
        echo "File size is non-zero";
else
        echo "File size is zero";
fi
 
Old 01-01-2006, 09:12 AM   #6
LiNuXkOlOnIe
Member
 
Registered: Dec 2005
Location: North germany
Distribution: Linux Mint
Posts: 45

Rep: Reputation: 16
Jaaauu. And the oscar goes tooo ...
jcliburn.

thank you.

Don't worry only spamming. :-)))
 
Old 01-01-2006, 09:35 AM   #7
jcliburn
Member
 
Registered: Dec 2003
Location: Mississippi, USA
Distribution: Fedora
Posts: 435

Rep: Reputation: 33
One caveat with the -s method... It returns false under TWO conditions: the file size is zero, OR if the file doesn't exist. So if you feed my sample script a nonexistent file name, it'll say "File size is zero".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a directory is empty? Aziz Linux - Software 2 09-27-2004 04:46 PM
check file permissions in a script cambie Linux - General 9 09-22-2004 12:13 AM
Script to check for contents in a file darin3200 Programming 7 04-25-2004 05:26 PM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 08:30 AM


All times are GMT -5. The time now is 01:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration