skip navigation

Content:: Finding Large Files

Finding Large Files with du
Last Modified: 2015-03-19

Using a few command line tools: du, sort and head we can find the large files and folders on a system.

Table of Contents
Finding Large Files
If we want to show the size of the current folders and sub folders we can run du with the -s and -h options.
-s will display the total for each argument
-h will print the sizes in "human readable" formats (e.g. 1K, 1M, 1G).
# du -sh
The output simply shows the total amount of space used
80G     .

Now if we would like to get a list of all of the files and subfolders we can use the -c, -h and -m options.
-c produce a grand total
-h --human-readable print sizes in human readable format
-m block size in Megabytes
# du -chm
1       ./.blackbox
80804   ./VirtualBox VMs

The first 8 characters in du -chm shows the size in megabytes. We can pipe the output through sort with the -k1,8 and -n and -r option.
-k option take the start end end position of "key" to sort through 1,8 will use the first 8 characters.
-n will do a numeric-sort
-r will sort it in reverse (highest to lowest)
du -chm|sort -k1,8 -nr
80970   total
80970   .
80804   ./VirtualBox VMs
1       ./.blackbox

Next we can pipe the output through head with the -n command
-n print the first X lines instead of the first 10.
# du -chm|sort -k1,8 -nr|head -n 20 The output will show the first 2 as total and "." local directory. Then the next largest 18 files, folders and subfolders.

If you are looking at an entire drive you can exclude certain folders with the du command using the --exclude option. # du -chm --exclude "mnt"|sort -k1,8 -nr|head -n 20
Using the heading path (/) at the beginning of the exlude option will not exclude the folder # du -chm --exclude "/mnt" |sort -k1,8 -nr | head -n 20