How To Check Linux Disk Space Usage: Classic SysAdmin Tips and Commands
This guide explains Linux commands and utilities for checking disk space and usage.
Various Linux commands and tools can help you manage disk drives and check for free space. This article will explain how to use these commands as well as the GNU Object Model Environment (GNOME) Disks utility.
Linux Disk Location
In Linux, everything is considered a file, even hardware devices. When a device connects to your computer, a device file is created in the /dev directory. Use the cd / command to change to the root filesystem, then use cd /dev to get into the /dev directory. The /dev directory contains the device files for all the devices, including disks and their drives (see Figure 1).
image001_7
Figure 1. Device files in Linux in the /dev directory.
List Disks in Linux Using the lsblk Command
You may find it helpful to use the lsblk command first to list the disks on a machine. Lsblk stands for “List block devices.” In Linux, disks and disk drives are known as block devices.
When we run the lsblk command without any other parameters, the output will include the Type column, which will reference the disk and any partitions available (see Figure 2). The output also displays columns for major and minor device numbers, removable device (RM), read-only device (RO), and a mount points column, which shows all locations where the device is mounted.
image002_4
Figure 2. The output of the lsblk command shows disks and partitions.
Linux Commands To Check Free Disk Space on a Drive
Most Linux distributions have built-in commands to check for free disk space per drive and in total.
Linux df command
The df command (“disk-free”) will display the available and used disk space, disk size, percentage of disk space used, and mount points.
Different switches can be used with the df command to modify the output format and display additional information.
df -h shows the output in a human-readable format.
df -a shows the filesystem’s complete disk usage, even if the available field (column) is set to 0.
You can use these two switches in the same command line (e.g., df -ha), as seen in Figure 3.
image003_6
Figure 3. The output of the df -ha command.
In Figure 3, the “Available” column displays the space available.
df -T shows the disk usage and displays a column for filesystem type.
df -i displays two columns for used and free inodes.
We can see the results of the output of df -i -T in Figure 4.
image004_2
Figure 4. The output from df -i -T.
Linux du command
The du command (“disk usage”) will display the disk usage of files and folders in the default kilobyte size.
As with the df command, we can add various switches to the du command:
du -h displays the output in human-readable format for all directories and subdirectories.
du -a shows disk usage for all files.
du -s displays the total disk space used by a particular file or directory.
In Figure 5, see the output of the du command in the terminal with the -h and -a switches added to the command line.
image005_2
Figure 5. The large output of the du -ha command.
If you run the du command without providing a directory, it will run in the directory you’re currently in. If you’d like to run the command in a different directory, you’ll need to specify it after the options. For example, du /var/log will display the disk usage of all the directories in /var/log, as seen in Figure 6.
image006_0
Figure 6. The command du /var/log will display the disk usage of all the directories in /var/log.
In Figure 7, we can see the output of the du command displayed in the terminal with the -s switch added to the command line.
image007_0
Figure 7. The output for how much disk space (in kilobytes) is being used by the home directory.
Graphical User Interface Tools: GNOME Disks Utility
GNOME is a free, open-source desktop environment for Linux and other Unix-like operating systems. If GNOME is installed, you can use its Disks utility to check for free space in each drive. (Note that other desktop environments are available for Linux and disk management utilities. We will look at only the GNOME Disks utility in this article.)
To open the GNOME Disks utility, type “Disks” into the GNOME search bar, as seen in Figure 8.
image008
Figure 8. Find the GNOME Disks utility by typing “disks” into the GNOME search bar.
Once Disks is open, you can drill down into drive and partition information and view the total disk size and disk space usage, as shown in Figure 9.
image009
Figure 9. GNOME Disks utility displays disk size and free space.
Why Do You Need to Check Disk Space on Your Linux Box?
You should track of and monitor disk space and utilization. In most cases, it is advisable to keep track of disk space and usage routinely. However, you may need to check disk space at any time.
Here are several common scenarios where you would check for free disk space and utilization.
Keep track of disk utilization: Checking disk usage helps ensure that partitions and directories are not growing too large.
Improve filesystem efficiency: Identifying and removing unnecessary or corrupted files can free up disk space and optimize the filesystem.
Ensure space for new files: You need sufficient disk space to create and download files and install software updates.
Monitor low disk space: Checking available disk space helps prevent the entire system from running out of space, which can lead to performance issues.
Confirm correct folder setup: Verifying available disk space ensures the proper configuration of folders for specific applications to function correctly.
Identify filesystem inefficiencies: Monitoring available disk space can provide insight into overall filesystem performance and identify inefficiencies.
Troubleshoot issues: Checking for free disk space can help when troubleshooting issues such as failed updates and operations.
Final Thoughts
Checking for free disk space on a Linux drive can be done easily using Linux commands and utilities. If you prefer graphical interfaces, you can use a GUI disk-checking utility, such as GNOME Disks
Frequently Asked Questions
Q: What is the df command in Linux?
A: The df ("disk free") command is used inside the terminal with various switches to check free and used disk space.
Q: What is the du command in Linux?
A: The du ("disk usage") command is used to check the disk usage of files and folders.
Q: How do I find out what takes up disk space in Linux?
A: You can use the df command. The output of df -H will show you the amount of space used vs. available, the percentage used, and the mount point of every disk attached to your system.
Additional Resources
Here are links to additional resources to help you on your way.
Linux Documentation
ITPro Today Linux Resources
Bonus Tip: Check for Disk Space Using a Python Script
You can programmatically check free space on disks using a Python script. Import the shutil (high-level operations on files) and psutil (process and system utilities) modules into the script. The script uses the disk_usage method from the shutil module to retrieve disk usage information.
image010
Figure 10. A simple Python script imports the shutil module from the Python external modules library and then uses the disk_usage method to check for disk usage.
The Python script calculates disk usage and saves it to a variable. The output, which includes total, used, and free disk space in bytes, is then printed to the terminal. The output shows the total, used, and free disk space in bytes.
image011
Figure 11. Use the python3 command in the Linux terminal to run the Python script called freeSpace.py (you can choose any name for your script).
Python Resources
About the Author
You May Also Like