Back

Listing disk usage of directories

I use this to quickly find biggest offenders of disk-space robbing. First of all du ("disk usage") needs to be used to print out the files under the chosen directory:

du $1

Unfortunately this prints every single file and its size in blocks of 1024 bytes. To make it more useful, we can add a -d 1 flag to limit directory-depth to 1 and -h for a human readable output of the file size:

du -h -d 1 $1

This is much more useful however the output is unsorted so it's not immediately obvious which directories take up most space. We can pipe du's ouput to sort:

du -h -d 1 $1 | sort

The output is sorted, however it's done lexicographically, meaning the human-readable sizes are misinterpreted as strings.. To sort the output by human-readable sizes, use the -h flag:

du -h -d 1 $1 | sort -h

Lastly, you can reverse the ordering of sort, to print the largest directories at the top. The final command looks like this:

du -h -d 1 $1 | sort -hr


Original publication date: 28/09/22 23:39:39
Last edit date: 28/06/23 19:57:08