Check disk space utilization in Linux

Let’s say your partition (for example, ‘/’) is getting full, but you don’t know where to look to take care of the issue. You can run the following that will give you a list of the directories sorted from the largest to smallest in GiB:

du -h / --max-depth 5 | grep -P '^[0-9\.]+G' | sort -n -r

The output of the command will look something like this:

17G     /
8.2G    /home
4.8G    /usr
2.2G    /usr/local/cpanel
2.2G    /usr/local
1.3G    /run/log/journal/8936098ee7011e65401ba43c5f52e098
1.3G    /run/log/journal
1.3G    /run/log
1.3G    /run
1.1G    /var
1.1G    /backup

In my case, I ran this from a server that doesn’t have much installed, but you get the idea. By default this command will check the ‘/’ partition/directory (du -h /), iterate 5 levels deep (–max-depth 5), and only display directories that are at least 1GiB in size (grep -P ‘^[0-9.]+G’). You can change up the values to fine tune the output to your needs. For example, lets say you want to start at the current directory, show 2 levels deep, and only show directories that are at least 1MiB in size, but under 1GiB:

du -h . --max-depth 2 | grep -P '^[0-9\.]+M' | sort -n -r

In this case, I was within ‘/backup’, so the output would look like this:

555M    ./2020-12-13
514M    ./2020-12-13/accounts
503M    ./monthly/2020-12-01
503M    ./monthly
41M     ./2020-12-13/system
12M     ./.meta

This is a very useful command that I personally utilize just about every day. Let me know how useful this has been for you in the comments.

Related Posts

Leave a Reply