How to compare file size in script?

Paul Howarth paul at city-fan.org
Mon Aug 15 16:15:35 UTC 2005


Hongwei Li wrote:
> Hi,
> 
> I want to write a script to compare file sizes.  My script is like this:
> 
> #!/bin/sh
> ls -l /tmp/test/t1 | awk '{print $5}' > thesize
> echo `cat thesize`
> if [ `cat thesize` > 300000 ]
> then
>         echo "big file: thesize"
>         exit 0
> fi
> 
> But, no matter how small or big the file t1 is, it always display:
> 
> 11190
> big file: thesize
> 
> The actual size of t1 is 11190.  I also tried the other code:
> 
> #!/bin/sh
> thissize=`ls -l /tmp/test/t1 | awk '{print $5}'`
> echo "$thissize"
> if [ $thissize > 300000 ]
> then
>         echo "big file: $thissize"
>         exit 0
> fi
> 
> then, it displays:
> 
> 11190
> big file: 11190
> 
> What's wrong in the codes?  How to fix it?

"man test" reveals that the comparison you want is "-gt", not ">", which 
will redirect output into a file called 300000 in this case.

Paul.




More information about the fedora-list mailing list