1827484 Members
2254 Online
109965 Solutions
New Discussion

Test file size

 
SOLVED
Go to solution
Adam Prince
Occasional Contributor

Test file size

G'day,

I would like to test a file size. If a file is equal to zero or 1 test would fail, and if a file was greater than 1 test would pass. I know there was be an easy way to do this. Thanks everyone for your help.
9 REPLIES 9
Craig Rants
Honored Contributor

Re: Test file size

If I read your question right:

NUM=`ll filename | awk '{print $7}'`
if [ $NUM -gt 0 ]
then
exec command1
else
exec command2
fi


MC,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor

Re: Test file size

Ooops,

NUM=`ll filename | awk '{print $7}'`
if [ $NUM -ge 1 ]
then
exec command1
else
exec command2
fi

Notice the ge and 1, I did not read it right the first time...
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor
Solution

Re: Test file size

Ok, last time

NUM=`ll filename | awk '{print $7}'`
if [ $NUM -le 1 ]
then
exec command1
else
exec command2
fi


My short term memory is shot today.

HH,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Justo Exposito
Esteemed Contributor

Re: Test file size

hi,

You can use find -size n[c] try to read the man page of find.

Regards,

Justo.
Help is a Beatiful word
Adam Prince
Occasional Contributor

Re: Test file size

Thanks for the help Craig!
The routine looks like this:
#!/bin/sh
NUM=`ls -l filename | awk '{print $5}'`
if [ "$NUM" -gt 1 ]
then
echo command1
else
echo command2
fi
Chris Vail
Honored Contributor

Re: Test file size

Here's an easy way:
SIZE=`ls -l $YOURFILE|awk '{ print $5 }'`
if test "$SIZE" -gt "1"
then
do something
else
do something else
fi

This will fail if the file doesn't exist, however. To test if the file exists AND THEN
test to see if its size is larger than 1 byte:
if test -f YOURFILE
then
insert the above script
fi

A. Clay Stephenson
Acclaimed Contributor

Re: Test file size

Hi:

You guys are doing it the hard way.

How about
if [ -s myfile ]
then
echo "File exists and has a size greater 0"
else
echo "File does not exist"
fi

Clay
If it ain't broke, I can fix that.
Mark Fenton
Esteemed Contributor

Re: Test file size

I'm with you Clay. The other script examples might be more useful if one were testing for a file of arbitrary size, but [ -s filename ] is much simpler.
Darrell Allen
Honored Contributor

Re: Test file size

Hi all,

Remember the question is whether the file size is greater than 1. Thus, [ -s myfile ] is inadequate as it tests for greater than 0.

:)

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)