Articles on: Virtual Private Servers & Dedicated Servers

Figure out Disk Space Usage on Linux

Looking for a Virtual Private Server or Dedicated Server? Take a look at VoltHosting's VPS or Dedicated Server plans.

Ran out of disk space on your Linux based machine? Thankfully you can figure out what is using up all the space! Follow this guide below to see what is using up all the space on your Linux based machine (such as Ubuntu).

The Disk Filesystem Command



Firstly, lets take a look at the df command to check disk space usage on a Linux system. You can use this command to show disk usage and free disk space for a specific file system.

Here’s an example of the output generated when running the df command on an Ubuntu server. I’ve added the -h argument, so the space usage is showing in a human-readable format.

root@volthosting-server:~# df -h

FilesystemSizeUsedAvailUse%Mounted on
udev71G071G0% /dev
tmpfs15G7.2M15G1%/run
/dev/mapper/pve-root94G85G4.4G96% /
tmpfs71G49M71G1%/dev/shm
tmpfs5.0M05.0M0%/run/lock
/dev/fuse128M36K128M1%/etc/pve
tmpfs15G015G0%/run/user/0

Note: it won't show in a table like this, this was done in the documentation to provide you with a mobile friendly viewing experience. But the outputted data will be similar to this.

The df command is excellent for listing all available hard drive mount points. However, you will generally only need to examine a specific mount (such as /) when searching for disk space usage. So, usually the du command is much more helpful.

The Disk Usage Command



All Ubuntu distributions have a disk usage ( du) command. This command-line tool estimates the disk usage in a specified directory. It does this by first summarizing any directory tree (the hierarchy of directories) in the given directory, followed by a usage summary of all the files in that directory.

Using the Disk Usage Command



Running the du command on a specific directory without any arguments will display the summary for that directory.

To ensure that the disk usage command has the required root user permissions to read the directories you are pointing it to, you can prefix any du command with sudo. (We've added the -h flag to keep the output in human readable format).
sudo du -h /home/volthosting/


Below is an example of the output you might get running the command to check the disk space:

sudo du -h /home/volthosting/

4.0K    /home/volthosting/.ssh
88K     /home/volthosting/webserver-stuff
8.7G    /home/volthosting/backups
1.2G    /home/volthosting/backups/rowans-things
4.0K    /home/volthosting/logs
4.0K    /home/volthosting/important-files
2.0K    /home/volthosting/important-files/really-cool-stuff


Here we can see all the directories in the /home/volthosting/ directory.

This will traverse through the directory tree for the home directory, showing the path to each directory it finds and summarizes the total disk usage of that directory.
Its important to note that this also shows hidden folders (beginning with a .). These usually aren't disk space hoggers, but worth keeping in mind.

Another way we can do this, is to include the -s argument, which will limit the results to the estimates for the top level directories only.

sudo du -sh /home/volthosting/


Below is an example of the output you might get running the command with the -sh flag:

sudo du -sh /home/volthosting/

4.0K    /home/volthosting/.ssh
88K     /home/volthosting/webserver-stuff
8.7G    /home/volthosting/backups
4.0K    /home/volthosting/logs
4.0K    /home/volthosting/important-files


Filtering the Results



Being able to filter the results returned is very helpful in order to see what actually is using up all the space! To do this, we can add a filter to the du output using the sort command.

We use the pipe | character to send the du output to the sort command, and then add the -h and -r arguments to it to sort the data by human-readable numbers, and reverse the results, showing largest to smallest.

We've also added the wildcard symbol * to the end of the du command to refine the results a little more, as it excludes the dot (hidden) files and folders, it does not return the total summary for the entire home directory.

sudo du -sh /home/volthosting/* | sort -hr


Below is an example of the output you might get running the command with the -sh flag:

sudo du -sh /home/volthosting/* | sort -hr

8.7G    /home/volthosting/backups
88K     /home/volthosting/webserver-stuff
4.0K    /home/volthosting/logs
4.0K    /home/volthosting/important-files


Hunting for the disk space users!



Now that we've learnt what the du and sort commands do, its time to put them together to find the space hog on your system.

Firstly, lets take a look at the root / directory on your server.

sudo du -sh /* | sort -hr


Depending on your server’s hard disk size and the amount of disk space in use, this could take a little while. Below is a simplified example of what the output might look like:
19.8G   /volthosting
1.9G    /var
1.7G    /usr
503M    /snap
145M    /boot
12M     /opt
7.8M    /etc
4.0M    /cache
3.0M    /home
1.6M    /root
992K    /run
64K     /tmp
16K     /lost+found
4.0K    /srv
4.0K    /mnt
4.0K    /media


As you can see, the /volthosting directory is taking up 19.8GB of space and is probably where the problem lies. So your next logical step would be to update the command to report on the /volthosting directory:

sudo du -sh /volthosting/* | sort -hr


This is an example of what this could return:

16.2G   /volthosting/volthosting-main-website
1.8G    /volthosting/development-files
1.0G    /volthosting/discord-bot
164M    /volthosting/misc


In this example, /volthosting/volthosting-main-website is taking up a whopping 16.2G of space. Next would be to see what’s happening inside /volthosting/volthosting-main-website:

sudo du -sh /volthosting/volthosting-main-website/* | sort -hr


You can continue running du in this way on subsequent folders until you find the culprit.

It will (probably not) be the /volthosting directory on your server taking up all the space. You should check for yourself which folder it actually is. For example, it could be /var or /opt.

Cleaning up the Files



Once you've located files or directories which you want to relate, firstly make sure that they are unneeded, or backed up somewhere else BEFORE deleting. Once a file is deleted, there is no coming back.

There is one command which does all the deleting, and that is the rm command.

For deleting a specific file:
sudo rm /volthosting/volthosting-main-website/a-big-file.txt

This command will delete the a-big-file.txt document.

For deleting a whole directory:
sudo rm -rf /volthosting/volthosting-main-website/logs

This will delete the logs directory.

The ncdu command



This command is the simplest of them all, but requires you to install additional software to your device.

Installing ncdu



You can install ncdu easily as its usually included in the default package repositories on most Linux distributions.

CommandSupported Linux distribution
sudo apt install ncduOn Debian, Ubuntu and Mint
sudo yum install ncduOn RHEL/CentOS/Fedora and Rocky/AlmaLinux
sudo emerge -a sys-apps/ncduOn Gentoo Linux
sudo apk add ncduOn Alpine Linux
sudo pacman -S ncduOn Arch Linux
sudo zypper install ncduOn OpenSUSE
sudo pkg install ncduOn FreeBSD


Using ncdu



Once installed, just type ncdu into the terminal to launch the program. It will firstly scan the number of files and directories, and provide the information on disk usage for the current directory.

Feel free to run this command in the / directory if you're unsure on where the disk hogs are.

It may show a screen like this for a little while, let it load and figure out the directory sizes.

Once, scanning completes, it will present the tree structure of files and folders along with their disk usage in human-readable format with a graphical bar presentation.



Use the arrow keys to navigate through directories and press i to see the selected directory information like full path, disk usage, apparent size. Again, hit i to hide the window.

This is the result if the i key is pressed while over the /var directory.

Now, find where your disk hog is, and delete the files! You can simply click d on the directory or file you want to delete, and press Yes to delete it.

Please make sure to check that the file is unneeded, or backed up. Once a file is deleted, there's no coming back!



To get more information about ncdu's options, press Shift+? and use the arrow keys to move up and down for more options.



To leave the interface, simply press q.

Updated on: 15/11/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!