Using the Find Command to Search for Files by Size

Understanding how to efficiently locate files by size on a Linux system is a critical task for both system administrators and casual users. In the following guide, we delve into the granular use of the Linux find command to search for files based on their size – a powerful skill for effective file management and disk space optimization.

In this tutorial you will learn:

  • How to search for files of a specific size
  • Using comparative size parameters to locate files
  • Examples showcasing practical uses of the find command for file sorting by size
Using the Find Command to Search for Files by Size
Using the Find Command to Search for Files by Size
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any GNU/Linux system
Software find
Other
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Unlocking the Potential of File Size Searches

Before diving into examples, it’s essential to comprehend the syntax variations and size parameters possible with find for accurate search results. Different suffixes designate various size measurements, making the tool versatile for diverse requirements.

  1. Finding Exactly Sized Files: Look for files precisely 6MB in your present directory.
    $ find . -size 6M

    Here, ‘M’ signifies Megabytes, equating to 1,048,576 bytes. Alternate suffixes include ‘b’ (512-byte blocks), ‘c’ (bytes), ‘w’ (two-byte words), ‘k’ (Kilobytes), ‘G’ (Gigabytes).

  2. Files Larger Than a Threshold: Search for files exceeding 2 Gigabytes in size.
    $ find . -size +2G

    The ‘+’ operator helps identify files bigger than the specified size.

  3. Finding Smaller Files: Locate all files under 10 Kilobytes.
    $ find . -size -10k

    Here, ‘-‘ implies we’re interested in files smaller than the given size parameter.

  4. Size Range Search: Discover files greater than 10MB yet smaller than 20MB.
    # find . -size +10M -size -20M

    Combine size parameters to define range searches effectively.

  5. List and File Size Display: Search /etc for files over 5MB and list them with sizes.
    $ find /etc -size +5M -exec ls -sh {} +

    The ‘-exec’ flag allows for additional commands, like ‘ls -sh’, to display file details.

  6. Top Three Largest Files: Identify the three largest documents in your directory, ranked by size.
    $ find . -type f -exec ls -s {} + | sort -n -r | head -3

    Combine ‘find’ with ‘sort’ and ‘head’ to pinpoint the largest files.



  7. Seeking the Smallest: Ferret out the tiniest files within your directory.
    $ find /etc/ -type f -exec ls -s {} + | sort -n | head -3

    This reverse approach seeks the smallest file assets.

  8. Zeroing in on Empty Files: Search for files that are completely empty.
    $ find . -type f -size 0b

    The ‘0b’ parameter is explicit for zero byte (empty) files. The ‘-empty’ option simplifies this task further.

  9. Finding Files of an Exact Size: Target files that are precisely 1024 bytes.
    $ find . -size 1024c

    The ‘c’ suffix illustrates a byte-exact search, optimal for certain data or logs.

  10. Deleting Large Temp Files: Clear out temporary files surpassing 100MB.
    $ find /path/to/tmp -type f -name '*.tmp' -size +100M -delete

    An ideal solution to manage disk space by purging substantial temp files.

  11. Enhanced File Listing: Present files over 1MB, with size and modification dates for easy review.
    $ find . -size +1M -exec ls -lh {} \; | awk '{ print $5, $6, $7, $9 }'

    The printout is more digestible and suitable for a manual check.

  12. Small File Permission Changes: Amend permissions for files lesser than 5KB.
    $ find . -size -5k -exec chmod 644 {} \;

    Modify permissions in bulk, enhancing your file security.

  13. Large File Quantification: Count all files in your directory over 500MB.
    $ find . -type f -size +500M | wc -l

    A handy tool for monitoring disk usage or preparing for cleanups.

Conclusion

The find command is a remarkable utility for managing files by their size on Linux systems. With the precise examples listed above, users can not only locate files but also conduct various operations such as deleting, changing permissions, or simply tabulating file sizes. Share your thoughts and any additional queries in the comments section below.



Comments and Discussions
Linux Forum