By using ls -lh we can get the file size.

Is there any way i can check if the file size is greater than 1MB then print a message like below ( i may have files with different sizes like 100mb , 1gb,10gb, 100kb )

if [ $FileSize > 1MB ];
    echo "File size is grater than 1MB"

Is there a way i can check the file size using IF statement , ( If this is already asked pls lemme know , as i didnt get any link to refer)

  • What's in $FileSize? A number of bytes, kilobytes, megabytes, or maybe kibibytes or mebibytes? Is it a string such as 1MiB, 10 k or 20 bytes? – Kusalananda Oct 25 '19 at 16:52
  • @Kusalananda. Thanks for the reply. Actually i may have file with different sizes 1gb,10gb,100mb , 100kb.... – Ravi Oct 25 '19 at 16:53

Using find on a specific file at $filepath:

if [ -n "$(find "$filepath" -prune -size +1000000c)" ]; then
    printf '%s is strictly larger than 1 MB\n' "$filepath"

This uses find to query the specific file at $filepath for its size. If the size is greater than 1000000 bytes, find will print the pathname of the file, otherwise it will generate nothing. The -n test is true if the string has non-zero length, which in this case means that find outputted something, which in turns means that the file is larger than 1 MB.

You didn't ask about this: Finding all regular files that are larger than 1 MB under some $dirpath and printing a short message for each:

find "$dirpath" -type f -size +1000000c \
    -exec printf '%s is larger than 1 MB\n' {} +

These pieces of code ought be to portable to any Unix.

Note also that using < or > in a test will test whether the two involved strings sort in a particular way lexicographically. These operators do not do numeric comparisons. For that, use -lt ("less than"), -le ("less than or equal to"), -gt ("greater than"), or -ge ("greater than or equal to"), -eq ("equal to"), or -ne ("not equal to"). These operators do integer comparisons.

  • Do we need to provide "+1000000c" -- "c" along with bytes ? – Ravi Oct 25 '19 at 17:51
  • @Ravi That is how you say 1 MB with standard find. GNU find may well understand -size +1M, as would find on e.g. macOS (but 1M actually means 1 MiB with these implementations, i.e. units of powers of 2 bytes rather than of powers of 10 bytes). – Kusalananda Oct 25 '19 at 19:41

If you want to query one single file at a time, stat is a general-purpose tool for querying various attributes of a particular file, including size among many others.

if [ $(stat -f %z "$FileName") -gt $FSize ]; then
    printf "File %s is larger than %d bytes.\n" "$FileName" $FSize

If what you're really looking for is a way to query a large number of files, and pull out the names of only those files which are over a certain size, then find may be what you want.

Given a group of files located in DIRECTORY, this will find those files larger than FSize bytes:

find $DIRECTORY -type f -size +$FSize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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