Linux Command Line: Find largest folders and print their size in human readable format

The command

If you want to find the largest files within your Linux system, see this post instead. Here is the command to find the top 20 largest folders in a given directory and print their size in a human readable format (eg. 2.4M, 24G).

 

# the full command
du -hx path/to/your/directory | sort -hr | head -20

 

Examples

Find the 20 heaviest directories in your ~/Documents directory and print their human readable size

# replace with the directory you want to scan
sudo du -hx ~/Documents | sort -hr | head -20

Find the top 10 heaviest directories in your entire system and print their human readable size.

# This might take a long time to execute, because you are scanning your entire system
sudo du -hx / | sort -hr | head -10

 

Step-by-step breakdown

You can try each of the following commands for yourself to see the results.

1- find

# find all directories within your current file system (-x) and print their size in a human-readable format (-h)
du -hx ~/Documents

2- sort

# sort by human-readable format (-h) so 2G will be bigger than 50k even if 2 < 5, and reverse the order (-r)
sudo du -hx ~/Documents | sort -hr

3- head -20

# keep the first 20 lines (-20)
sudo du -hx ~/Documents | sort -hr