Testing for empty file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Testing for empty file
# 1  
Old 11-14-2008
Testing for empty file

Hello,

I need to determine if a file I have is empty or not. How can I go about doing this in shell scripting? Some sample code would be appreciated?

Thanks,
# 2  
Old 11-14-2008
Code:
FILE=/path/to/file
if [[ -s $FILE ]] ; then
         echo "Greater"
else
         echo "Not"
fi

# 3  
Old 11-14-2008
Code:
if [[ -s myfilename ]] ; then
     echo "my file exists and is greater than zero length"
else
     echo "my file does not exist or it is zero length"
fi

# 4  
Old 11-14-2008
What is wrong with this syntax?

Trying to debug it myself but running into a jam. The interpreter does not seem to like this code:

Code:
if [[ -s results1.tmp ]] & [[-s results2.tmp]] 
then 
     /bin/mail -s "Illegal Loggon Attempts on MAIL" sysadmin@swi.com < final_email.tmp
else
    if [[-s results1.tmp]]  
    then    
        /bin/mail -s "Illegal Loggon Attempts on MAIL" sysadmin@swi.com < email.tmp
    fi
else
     if [[-s results2.tmp]]
     then    
         /bin/mail -s "Illegal Loggon Attempts on MAIL" sysadmin@swi.com < email1.tmp
    fi
fi

# 5  
Old 11-14-2008
Im not sure how often you plan on running this, you will get lots of emails if you run it often.

I use logwatch that sends me an email daily with all this, and more info.
# 6  
Old 11-14-2008
Quote:
Originally Posted by Ikon
Im not sure how often you plan on running this, you will get lots of emails if you run it often.

I use logwatch that sends me an email daily with all this, and more info.
I will run it daily so we will not be getting more than one email. Just need to debug that if statement above and I am done.....
# 7  
Old 11-14-2008
What error do you get??

I beleive if should be 2 && like below.
Code:
if [ -s results1.tmp ] && [-s results2.tmp]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

File and directory testing

original post -- I have a korn shell script that does some things that depend on creating and writing a file in a directory. I'm looking for a more elegant/efficient way to do the check than what I'm using now: if ] then print "Creating ${STGDIR}/${SHOW}" mkdir... (3 Replies)
Discussion started by: Dalej
3 Replies

3. Shell Programming and Scripting

Testing for one word in a file

I am trying to test the output of a file. What I have so far is this: if ]; then echo "yes";fi The problem with this is it works but I only want it to print out if the file contains the word "Compacted." The == sign means identical or equal to so it won't work. I tried ~ but that doesn't... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. Shell Programming and Scripting

testing file permissions.....

script name: filetest.sh if ; then echo " You didn't enter any argument" elif ; then echo " file not exist" elif ; then echo " file not readable" elif ; then echo " file not writable" else echo " file both readable and writable" fi running like... $ ./filetest filename ... (3 Replies)
Discussion started by: ani83_pune
3 Replies

5. Shell Programming and Scripting

testing if a file is a directory

i have written this simple script called isdir.sh #! /bin/bash dir=$1 _ls=`ls $dir` for file in $_ls do if then echo "D $file" fi donethe output is not right. for example $ ./isdir.sh src ***no output*** but i have in ~/src some directories drwxr-xr-x 2... (5 Replies)
Discussion started by: and77
5 Replies

6. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

7. UNIX for Dummies Questions & Answers

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies

8. UNIX for Advanced & Expert Users

empty file in hp-ux

Hi, I need your help. How can I create an empty filename with a specific size, in hp-ux? Regards, Mizi (2 Replies)
Discussion started by: Mizi
2 Replies

9. UNIX for Dummies Questions & Answers

Testing existence of a file /directory

hey guys How can i test existence of a file /directory in a directory in a script thanks (2 Replies)
Discussion started by: ajaya
2 Replies

10. UNIX for Dummies Questions & Answers

testing for file size in script

Has anyone got a few tips on how I can test if the file size is 0? I am moving files on a regular basis from one location to another with ftp. The files which are 0 bytes in size we want to discard. Thankyou in advance. (3 Replies)
Discussion started by: Ivo
3 Replies
Login or Register to Ask a Question