How to Use the ‘find’ Command to Search for Files by Size in Linux

When managing a Linux server or desktop, you will inevitably encounter situations where your storage drive begins to run out of space. While tools like df and du are excellent for providing a high-level overview of disk usage, they do not help you identify specific massive files hidden deep within your directory structure.

To pinpoint the exact files consuming your storage, you can use the highly versatile find command combined with the -size flag. This allows you to quickly locate files that exceed a certain threshold, such as log files that have grown out of control or forgotten video backups.

Understanding the Syntax

The basic syntax for searching by file size is:

find [directory] -size [modifier][size][unit]

The modifier tells the command whether you are looking for files larger than (+), smaller than (-), or exactly equal to the specified size. The unit indicates whether you are searching in Megabytes (M), Gigabytes (G), Kilobytes (k), or Bytes (c). Note that Linux commands are case-sensitive; Megabytes must be a capital ‘M’, while Kilobytes must be a lowercase ‘k’.

How to Find Files Larger Than a Specific Size

To find massive files, you will use the plus (+) modifier. For example, if you want to search your entire system starting from the root directory (/) for any file larger than 500 Megabytes, you would execute:

sudo find / -type f -size +500M

Note: We use sudo when searching the entire root directory to avoid a screen filled with “Permission denied” errors for system folders you cannot normally access. The -type f flag ensures we are only looking for actual files, not directories.

To search for files larger than 2 Gigabytes within your personal home directory, use:

find ~/ -type f -size +2G

How to Find Files Smaller Than a Specific Size

Conversely, you can use the minus (-) modifier to find small files. This is often useful for finding empty or near-empty configuration files. To search the /etc directory for files smaller than 10 Kilobytes, use:

find /etc -type f -size -10k

To find files that are exactly empty (zero bytes), it is better to use the dedicated -empty flag rather than the size flag:

find ~/ -type f -empty

How to Search Within a Specific Size Range

The true power of the find command becomes apparent when you chain flags together. If you want to find files that fall within a specific size bracket—for example, files larger than 100 Megabytes but smaller than 500 Megabytes—you simply provide the -size argument twice.

find /var/log -type f -size +100M -size -500M

Executing Commands on the Discovered Files

Once you have located the large files consuming your space, you can use the -exec flag to perform an action on them immediately, such as deleting them or moving them to a backup drive.

For example, to find all .log files in /var/log that are larger than 1 Gigabyte and instantly delete them, you would execute:

sudo find /var/log -type f -name "*.log" -size +1G -exec rm {} \;

Warning: Use the -exec rm combination with extreme caution. It is highly recommended to run the find command without the -exec portion first to review the list of files and ensure you are not about to delete something vital to your system.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.