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 to 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 an if statement?


2 Answers 2


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
    Commented Oct 25, 2019 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
    Commented Oct 25, 2019 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
  • 1
    What is stat -f %z? on my system it is stat -c %s for filesize in bytes
    – qwr
    Commented Aug 4, 2022 at 7:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .