Finding Large Files using Unix/Linux (and Mac)

Sometimes you really want to know where your heavy files are so that you can free up some disk space.  Using the right command can certainly make this a less challenging task.  Here are a variety of commands that can help you to accomplish such a task.  

The Linux Cookbook teaches the following techniques: 

Finding the Largest Files in a Directory

To find the largest files in a given directory, use ls to list its contents with the `-S’ option, which sorts files in descending order by their size (normally, ls outputs files sorted alphabetically). Include the `-l’option to output the size and other file attributes.

To list the files in the current directory, with their attributes, sorted with the largest files first, type:

$ ls -lS

Finding the Largest Directories

Use the `-r’ option with sort to reverse the listing and output the largest directories first.

To output a list of the subdirectories in the current directory tree, sorted in descending order by size, type:

$ du -S . | sort -nr

To output a list of the subdirectories in the `/usr/local’ directory tree, sorted in descending order by size, type:

$ du -S /usr/local | sort -nr

AP Lawrence suggests some other useful tactics:

Although time consuming, the following procedure can be used to track down where your space has been used.

$ cd /

$ du -s *

(Some folks like to use “du -cks *”, which is easy to remember as “ducks”. For “human readable”, add an “h” to the end of that.)

This command will print the number of blocks used by each directory and file in the root. Most likely, the largest number printed is where you should be looking. You can then cd to that directory, and examine it. If it has sub-directories, you might use:

$ find . -type d -exec du -s {} \;

You can search for “large” files by cd’ing to a suspect directory ( or even starting at /, if you must), and typing:

$ find . -size +5000 -print

which will print the names of all files over 5,000 blocks (2,560,000) bytes. This may find many, many files, so you might want to refine it with larger numbers. You might also want to sort it:

$ find / -size +2000 -exec ls -s {} \; | sort -nr | more

To limit find to the filesystem you are on, add “-mount”:

$ find . -mount -size +5000 -print

If you are using Mac OS X:

$ mdfind 'kMDItemFSSize > 20000000'

will find files over 20,000,000 bytes.

If you have any other techniques that you like to use, please post them in the comments.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.