How to Check and Manage Disk Space in Linux

One essential skill that system administrators need is to maintain the health of both online and offline systems. This is especially important on production servers where downtime or problems can cause loss of data. A common issue is that updates fail due to a lack of disk space, but there are some simple checks you can make when faced with an error message in order to keep critical systems running smoothly.

There are primarily two commands that can be used:

df – This reports the amount of disk space on a system

du – This shows the amount of space used by specific files

Each of the above are for different checks and can be combined if required. Below are some examples to illustrate their use.

Using the df command

Open the Terminal and type df, then press Enter. It should produce an output that looks like the following image.

As you can see, it shows every disk that is connected to the system. This can be cumbersome, so users can narrow things down by specifying the disk they are working on. In my examples my main disk is reported as “/dev/sda” – I have also included specific partitions with “/dev/sda1″ and /”dev/sda2.”

df can be made a little easier to read by typing df -h

You can narrow things down further by adding the --output flag. The parameters for this command are:

source – source of the device mount point

size – total number of blocks

used – total number of used blocks

avail – total number of available blocks

pcent – percentage of used space

target – mount point for the device

In this example I have used just the two parameters.

df--output=source,used,avail

Using the du command

So you have discovered that the disk is virtually full, but what is causing it? This is where du can show those problem files. In a real-world example at my job we discovered a remote server was at 98% disk space without any real warning or reason. It turned out that there were masses of java error log files that were a total size of around 40GB. That is a lot of space that didn’t need to be taken, and so after using du we were able to clear the files.

As a word of warning, if you simply type du, it will return all files, and that can take a massive amount of time. It is better to specify some parameters. Let’s look for the top ten largest directories on the system.

du-a/|sort-n-r|head-n10

It will scan and produce a similar type of result as this. Ignore the permissions warning for now.

du alternative: ncdu

If you prefer a more interactive way to see your hard disk information, ncdu provides an ncurses-based interface to du. It displays the same information but in a more intuitive way. It also allows you to navigate among the different folders using the arrow keys and using the Enter key to make a selection.

You can install it in Ubuntu with the command:

sudo apt install ncdu

To use it, type:

ncdu /directory-to-scan

Replace the “directory-to-scan” with the actual directory you want to scan. For example, to scan the whole hard disk, we can type:

ncdu /

Good Housekeeping

In addition to the commands above, there are some basic things users can do to help keep their disk usage down to a minimum.

Autoremove

The most obvious one on Ubuntu-based systems is to check for outdated packages. Within the Terminal you can type:

sudo apt autoremove

Once the password has been entered correctly, the system will start to delete orphaned packages. It can also remove older kernels which take up space and are largely unnecessary.

Clear the APT cache

When Ubuntu downloads packages, it keeps the apt files so they can be re-installed easily if needed. These take up space that can be reclaimed with:

sudodu-sh/var/cache/apt

Now you can clean this as follows:

sudo apt autoclean

GUI-Based Options

What if all this Terminal work seems like too much effort, or you just don’t like messing with the command line in case of damage to the system? Thankfully, Linux has some GUI-based tools that can be used.

Bleachbit

This is a program that is available for both Windows and Linux systems, and it operates in the same manner. It can be downloaded from the website, but is in most system repositories. Once installed, it works on a simple checkbox selection and afterwards you can run the tool.