Discrepancy in file size on disk and ls output

Posted by smokinguns on Super User See other posts from Super User or by smokinguns
Published on 2012-10-08T18:48:49Z Indexed on 2012/10/08 21:40 UTC
Read the original article Hit count: 208

Filed under:
|
|

I have a script that checks for gzipped file sizes greater than 1MB and outputs files along with their sizes as a report.

This is the code:

myReport=`ls -ltrh "$somePath" | egrep '\.gz$' |  awk '{print $9,"=>",$5}'`
# Count files that exceed 1MB
oversizeFiles=`find "$somePath" -maxdepth 1  -size +1M -iname "*.gz" -print0 | xargs -0 ls -lh | wc -l`

 if [ $oversizeFiles -eq 0 ];then
    status="PASS"

 else
     status="CHECK FAILED. FOUND FILES GREATER THAN 1MB"
 fi

echo -e $status"\n"$myReport

The problem is that ls command outputs the files sizes as 1.0MB in the report but the status is "FAIL" as "$oversizeFiles" variable's value is 2. I checked the file sizes on disk and 2 files are 1.1MB. Why this discrepancy? How should I modify the script so that I can generate an accurate report?

BTW, I'm on a Mac.

Here is what man page for "find" says on my Mac OSX:

-size n[ckMGTP]
True if the file's size, rounded up, in 512-byte blocks is n.  
If n is followed by a c,then the primary is true if the file's size is n bytes (characters).  
Similarly if n is followed by a scale indicator then the file's size is compared to n scaled as:  

 k       kilobytes (1024 bytes)
 M       megabytes (1024 kilobytes)
 G       gigabytes (1024 megabytes)
 T       terabytes (1024 gigabytes)
 P       petabytes (1024 terabytes)

© Super User or respective owner

Related posts about mac

Related posts about shell-script