Find out disk usage on Linux with du

One day, you may find out that all free space has been consumed on Linux. The problem is, what seems to be causing it? It could be logs or a large installation of a package. It may even be from compiling a large piece of software. There is a program that can help sort files/folders from largest to smallest by using the du disk usage utility.

Du for disk usage

Du stands for disk usage. It is a utility that outputs every folder/file and how much space it consumes on the hard drive. Du is installed by default in every Linux distribution available. We are now going to list our files/folders; use this to list every single file/folder in your system.

sudo du

This will list every file and folder within the system. By using this command on its own, it may be too much output for your shell terminal. Fortunately, we can use other commands in our arsenal to figure out our little disk usage fiasco. Now lets do some sorting with du to help organize our lists.

Why don’t we go ahead and sort out the results produced by du with:

sudo du -a | sort -n -r

Our du disk usage utility has displayed the output and sorted it by reverse(-n) and numerically(-r). Unfortunately that still won’t be enough and the whole disk will be displayed, we will now need to add another command to the mix by doing this:

sudo du -a | sort -n -r | head -n 10
du disk usage utility in Linux

You will now notice that the head command has been added. This limits the output to a certain number as defined right after the -n. Du -a displays all files and folders, sorts it by reverse and numeral order, and displays only 10 entries.

You should now have a good idea on what seems to be consuming all the free disk space. In most cases, logs can prove to be the culprit. Usually segregating logs onto a dedicated server would be best practice.