September 26, 2019
4 min Read
September 26, 2019
4 min Read
In this tutorial, we are going to show you how to use the df command to check disk space in Linux, and the du command to monitor disk usage. If you’re a Linux user, these two essential tools will help you manage your files more effectively.
df and du commands have a slightly different purpose when analyzing a hard drive. In order to avoid confusion, we’ll explain them in separate sections. Let’s begin with the df command!
df, which stands for Disk Filesystem, is used to check disk space. It will display available and used storage of file systems on your machine.
When executing this command, you will see the default columns: Filesystem, Size, Used, Available, Use%, and Mounted On. It should look something like this:
By adding a certain option to the df command, you can check the disk space in Linux more precisely. These are the most popular options:
Another important command is du, short for Disk Usage. It will show you details about the disk usage of files and directories on a Linux computer or server. With the du command, you need to specify which folder or file you want to check. The syntax is as follow:
du <options> <location of directory or file>
Let’s take a look at real-world use of the du command with the Desktop directory:
You can get more information done by combining df and du command with other arguments. By doing this, you will get a better idea of which files you can delete to free up disk space.
Just remember to start with df command to see which file system needs a cleanup the most. After that, you can proceed with these combinations.
First, we gather files and folders on the Desktop in a readable format using the du command. Then, we pipe the result to the sort command together with the -rn option. The script will sort all the files and folders from largest to smallest to check the disk space use in Linux. The combination should look like this:
du -h /home/user/Desktop | sort -rn
Remember that you shouldn’t necessarily delete files just because they are large. If you’re not cautious, you might delete essential files that would break your project.
Let’s say you want to see all files that are above a certain size. The most effective way to do that is by using the command below:
du -h /home/user/Desktop | grep '^\s*[0-9\.]\+G'
The grep command allows us to search for files based on a specified pattern. In this example, the script will return with any files bigger than 1 GB. If you want to single out 1 MB+ data, you can replace G with M.
The last combination is useful when you need to exclude a particular file format from the search results. For instance:
du -h /home/user/Desktop/ --exclude="*.txt"
The –exclude=”.txt” argument makes sure the du command will display all file formats except for .txt documents.
df and du commands are file management tools that will check disk space in Linux and display all stored files on your machine. You are allowed to add certain options (like -h, -m, -k, etc.) to refine the output based on your needs.
What’s great, users can get a more specific result by combining du and df with other commands, such as sort, grep, and exclude. Together, they will help you better understand how disk space is used on your server. Be sure to check out our article for more useful Linux commands.
If you have any questions, feel free to comment down below!