Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)
share|improve this question

1 Answer 1

In find, M actually refers to mebibytes, not megabytes.

find -size +1M

will find all files greater than 1,048,576 bytes.

To find all files greater than 1.0 MB (1,000,000 bytes), use this instead:

find -size +1000000c
share|improve this answer
    
Actually, 1048065 or more bytes, since the size rounded up to the next full 512 byte block. –  Daniel Beck Oct 8 '12 at 19:27
    
ls considers files to be 1.0M up to 1101004 bytes, and that is significantly more (51,7KiB) than find's minimum required size of 1048065. –  Daniel Beck Oct 8 '12 at 19:43
    
@DanielBeck: I was was wrong about "greater or equal", but - at least on Ubuntu - there appears to be no rounding. I checked: find -size 1048574c finds a file of 1048575 bytes and find -size 1M finds a file of 1048577 bytes. Regarding your second comment, I'm not sure I checked the file sizes on disk... refers to ls. –  Dennis Oct 8 '12 at 21:01
    
This Q is about OS X/BSD find. There's a difference in how numerical arguments are intepreted IIRC: All primaries which take a numeric argument allow the number to be preceded by a plus sign (+) or a minus sign (-). A preceding plus sign means more than n, a preceding minus sign means less than n and neither means exactly n.. man find also states regarding -size: 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).. 2nd cmt just indicates how much these values overlap. –  Daniel Beck Oct 9 '12 at 5:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.