The find command is really useful for locating files and directories etc on *nix and Mac. I recently needed to find all files greater than 1MB but it wasn’t all that clear how to do this from the find man page so I’ve written this post to show how to do it.
Using the -size flag
The -size flag is used to find files of a specific size using k for kilobytes, M for megabytes, G for gigabytes, T for terabytes and P for petabytes. However on its own the size specified needs to be an exact match.
So doing this, for example, will find all the files in the current directory and below that are exactly 1 megabyte:
find . -type f -size 1M
I couldn’t see anything in the manpage that allowed a search for files greater or less than the size specific but somehow managed to work out it’s possible to prefix the number with a + or – like so:
find . -type f -size +1M
or to find files less than 1 megabyte:
find . -type f -size -1M
Note that if the file is exactly 1 megabyte then neither -size +1M nor -size -1M will find it, but -size 1M will. If it’s 1048577 bytes (i.e. 1 megabyte + 1 byte), then -size +1M will find it.
Using multiple -size flags
Thanks to a question in the comments section below, I’ve added this update. If you wanted to find files between e.g. 1MB and 2MB you can use multiple -size flags as shown in this example:
find . -type f -size +1M -size -2M
Find zero length files
See my related post titled "using find to locate all zero length files" to find zero length files.