Find largest files in a directory using the terminal

June 1, 2016

If you need to find out what the largest files are in a directory and subdirectories here is a command you can run that will find the top 5 largest files.

find . -type f | sed 's/.*/"&"/' | xargs ls -Slh | head -n 5

Breakdown:

  1. find . -type f: Find all files in this directory and below
  2. sed ‘s/.*/”&”/’: Put double quotes around the files. xargs can handle this itself, however not all implementations do, this method is safer.
  3. xargs ls -Slh: Using the filenames from 1 list them all sorted by size.
  4. head -n 5: Show the output of the first 5 rows. Which becomes the 5 largest files because ls sorted the output.
Leave a Reply

Your email address will not be published. Required fields are marked *