How to Check Disk Usage in Linux
No matter how big your storage drive is, you always need to see how much space you’ve used and what files are using it. While there are ways to do this from the GUI on Linux, as always you get more detail using the terminal command line. Terminal tools are particularly useful for users monitoring remote systems, such as busy system administrators logging in through a secure shell. link. In this tutorial, we will look at different ways to use the terminal to check disk usage, which will allow us to see how much storage space is left on our system, wherever we are.
All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Using df to check disk usage in Linux
A simple approach to check all disk usage on a system is to simply issue the df command from the home directory in a terminal window.
You should see a long output list. This list of items includes not only physical hard disks connected to your system, but also all separate disk partitions, as well as virtual disks and temporary disks in your system.
To locate physical disks or partitions on physical disks, look for listings beginning with /dev/ such as /dev/sda1/. In this case, dev stands for device and indicates that the item following it is a physical drive or a partition on a physical drive. Finally, you will notice that, by default, the df command displays the size of the drive or partition in kilobytes.
Make the df command easier to read
The raw output generated by the df the order can be overwhelming. We can add a few simple arguments to the df command to make the output a bit easier to read and understand.
1. Use the -h argument to display sizes and usage in megabytes and gigabytes. the -h the argument refers to “human readable”.
2. Utilize df-h/ to see an overview of disk usage. This command will show disk usage for the main filesystem (/)
df -h /
3. Pass df-h plus the location of a drive to see information about this drive only. On our machine, our primary physical drive is mounted at /dev/sda2/ which is common but may be different on your system.
df -h /dev/sda2/
On another test system, our NVMe drive was located at /dev/nvme0n1p6 Added location to df allows us to explore a single area of a filesystem or a specific drive, making the output more focused and readable.
Specify df results by filesystem type
By using the -t In combination with other arguments, we can only return results for a specific type of filesystem. For example FAT, NTFS, ext, etc.
1. List the results of df but only for ext4 filesystems.
df -t ext4
On our example system, our main physical disk filesystem is ext4, but you can use any filesystem name you might have on your own system, such as FAT, exFAT, ext3, ext2, etc. .
2. Run the command again adding the -h argument. It is important that the you the argument is placed at the end of the argument list as you The argument expects to be followed by a filesystem type. If for example you want to add the -h argument, you must add it before the you as shown below.
df -ht ext4
Exploring Linux disk usage with the du command
Often we would just want to check how much space a file or directory is using. the of The command is useful for this and works the same as the command df command we looked at earlier. In the following examples, we have used our Music directory which contains 3 subdirectories containing mp3s. You can use any directory with content on your system to try these commands.
1. Navigate to a directory, list its contents, and then check disk usage.
cd Music ls du
Note that, like the df command, in its standard form, the du command returns values in kilobytes. The output of this command is quite simple: it lists the size of each subdirectory, then the name, then finally, on the last line, returns the sum of all the subdirectory sizes to give the disk usage in the directory.
2. Add the -h argument To do of returns results in megabytes and gigabytes.
3. Simplify the output to show only the total directory size. If we just want to know the total size of the directory and its subdirectories, adding -s as argument will just return the total.
4. Specify a location for the of ordered.
cd du -hs Music
As with other Linux commands, we don’t always need to run the du command from the target directory, instead we can specify a path to a target directory. It can be a relative path, from our current location, or an absolute path.
The df and du commands are deceptively simple but extremely powerful. It doesn’t matter if you are a newcomer or a seasoned system administrator, both commands are absolutely essential to keep your Linux machines running smoothly. At the basic level, each command gives you the information you need. With the addition of our suggested arguments, these commands are ready to become part of your sysadmin toolkit.