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.

Grant Knoetze, Contributor

June 6, 2023

9 Min Read
logos of linux penguin in a heap on a table
Alamy

A variety of Linux commands and tools can help you manage disk drives and check for free space. In this article, I 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 is connected to your computer, a device file is created in the /dev directory. Use the cd / command to first 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).

Device files in Linux in the /dev directory

image001_7

Figure 1. Device files in Linux in the /dev directory.

List Disks in Linux Using the lsblk Command

It may be useful to first use the lsblk command 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 on it (see Figure 2). The output also displays columns for major and minor device number, removable device (RM), read-only device (RO), and a mountpoints column, which displays all locations where the device is mounted.

The output of lsblk command shows disk and partitions

image002_4

Figure 2. The output of 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 mountpoints.

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.

  • These two switches can be used in the same command line – e.g., df -ha, as can be seen in Figure 3.

The output of the df -ha command

image003_6

Figure 3. The output of the df -ha command.

As we can see in Figure 3, the “Available” column displays the amount of space available.

  • df -T shows the disk usage and displays a column for filesystem type.

  • df -i displays an additional two columns, for used and free inodes.

We can see the results of the output of df -i -T in Figure 4.

The output from df -i -T

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, the output of the du command is displayed in the terminal with the -h and -a switches added to the command line.

The large output of the du -ha command

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 can be seen in Figure 6.

The output of du /var/log

image006_0

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.

The output for how much disk space (in kilobytes) is being used by the home directory

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 and 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, as well as other 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 can be seen in Figure 8.

Find the GNOME Disks utility by typing “disks” into the GNOME search bar

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.png

image009

Figure 9. GNOME Disks utility displays disk size and free space.

Why Do You Need to Check Disk Space on Your Linux Box?

It’s important to keep 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: Sufficient disk space is required for creating new files, installing software updates, and downloading new files.

  • 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 that folders are properly configured 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 failed operations.

Final Thoughts

Checking for free disk space on a Linux drive can be done easily using Linux commands and utilities. For users who prefer graphical interfaces rather than working in the terminal, you can use a GUI disk-checking utility, such as GNOME Disks, to get the job done.

Frequently Asked Questions

What is the "df" command in Linux?

The df command stands for “disk free” and is used inside the terminal with various switches to check free and used disk space.

What is the “du” command in Linux?

The du command stands for “disk usage” and is used to check the disk usage of files and folders.

How do I find out what is taking up disk space in Linux?

You can use the df command. The output of df -H will show you how much space is used and available, the percentage used, and the mountpoint 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. To do so, 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.

A Python script imports the shutil module from the Python external modules library, then uses the disk_usage method to check disk usage

image010

Figure 10. A simple Python script imports the shutil module from the Python external modules library, 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.

Use the python3 command in the Linux terminal to run the Python script called freeSpace.py (you can choose any name for your script)

image011

Figure 10. 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(s)

Grant Knoetze

Contributor

Grant Knoetze is a cybersecurity analyst with a special interest in DFIR, programming languages, incident response, red-teaming, and malware analysis. His full-time job includes teaching and instructing in various topics from basic Linux all the way through to malware incident response, and other advanced topics. He is also a speaker at various conferences worldwide.

www.grantknoetze.com

https://github.com/Grant-Knoetze

https://www.linkedin.com/in/grant-knoetze-563b0b1b6/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like