- Edited
Introduction
In this tutorial, we'll walk you through how to identify what’s consuming space on your server and where it’s located. We’ll use two commands, df and du, which allow you to examine the free space on mounted file systems and see how much space individual directories are using.
Though this tutorial uses Ubuntu 16.04, you can follow these steps for any distribution available on BuyCheapVPS.
Update System
As a first step, we always recommend updating your server. You can do this easily by running one of the following commands, depending on your package manager:
apt-get update
or
yum update
Using the df Command
The df command (short for "disk free") is a standard Unix tool used to display available disk space on file systems that the user has permission to read. When you run df, it shows the space usage of all mounted file systems. Here’s a sample output from the df command without any options:
df
The first column, "Filesystem," shows the storage device (e.g., /dev/simfs). The second column displays the filesystem size in kilobytes, while the third and fourth columns show how much space is used and how much is free, respectively. The next column displays the usage percentage, and the final column, "Mounted on," shows where the file system is mounted.
To make the output more readable, use the -h option, which will display the sizes in a human-readable format (e.g., MB, GB):
df -h
Other useful options for df include:
-T to show the filesystem type in an additional column.
-l to display only local filesystems, excluding remote filesystems mounted via NFS.
Using the du Command
While df gives you a broad overview, the du (disk usage) command provides a more detailed view of what’s consuming disk space. Use du to see how much space a directory is taking up:
du
For a more readable output, use the -h option:
du -h
To see the total disk usage without the detailed breakdown of subdirectories, add the -s flag:
du -hs
If you’ve used one of the above du commands, you’ve probably noticed that the output is extensive, as it shows the space usage of all directories and their subdirectories. If you want a more concise view, showing the space used only by top-level folders (without drilling down into deeper subdirectories), you can use the following command:
du --max-depth=1 | sort -n | awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}'
Conclusion
The df and du commands are powerful tools for monitoring disk space usage, helping you identify which files and directories are consuming the most storage. There are many other options you can explore by checking the help and man pages of these commands for more advanced use cases.