LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-24-2004, 08:11 PM   #1
john8675309
Member
 
Registered: Oct 2003
Location: Spencer, Iowa
Distribution: Slackware (of course)
Posts: 67

Rep: Reputation: 16
Bash Scripting Empty file


How can I detect if a file is empty using bash?

--John
 
Old 12-24-2004, 08:18 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Code:
if [ ! -s $file ]
    print "$file is empty"
else
    print "$file has content"
fi

Last edited by bulliver; 12-24-2004 at 08:20 PM.
 
Old 08-08-2007, 04:05 PM   #3
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
1. `then' is missing
2. `print' should be `echo':

Code:
file=/etc/passwd
if [ ! -s $file ]; then
    echo "$file is empty"
else
    echo "$file has content"
fi
You can even use a one-liner:

Code:
file=/etc/passwd
[ -s $file ] && echo "$file has content" || echo "$file is empty"
Freddy Vulto
http://fvue.nl/wiki/Bash
 
  


Reply



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
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM
bash scripting - how to make config file? Neorio Programming 3 10-22-2004 06:39 AM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM
bash scripting - largest # file extension brian0918 Programming 3 06-16-2003 12:06 PM
bash scripting/batch file type thing antken Linux - General 2 01-22-2001 07:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration