Linux Commands

How to Check the Disk Space in Linux from the Command Line

When working with Linux, you must quickly check the available space on your system. That way, you can keep a tab on your drives to ensure that you don’t run out of space. Checking the disk space is straightforward, depending on your GUI desktop. However, when you have a headless server, you need a way to check your disk space using the command line.

This guide focuses on checking the disk space in Linux via the command line.

Checking the Disk Space in Linux Using DU and DF Linux Commands

Linux has two useful commands to check the disk space on your system. The df (disk free) command displays the available disk space on your system. That way, you can keep a tab on how much space is left on your system.

The du (disk usage) command gives the directories that take up your disk space. Unlike df, the du command focuses on the directories that consume space, while the df command focuses on the specific drives and the space they use.

Working with DF

The df command has different options when checking the available disk space. Running the command with no option displays an information about the available file systems, their size, the space used, the remaining space, the used space as a percentage, and the mount point for the file system.

$ df

The output that we get is not easily readable. However, we can add the -h flag to get a human-readable output.

$ df -h

In the output, our main drive is the /dev/sda3. It has a disk size of 24 Gigabytes and is 54% used. Suppose you had more drives mounted on your system. You could get a more detailed output with all the file systems displayed and their disk space.

The df command also lets you specify the target disk drive. For instance, let’s specify our main drive to retrieve its details.

$ df -h /dev/sda3


This way, we narrowed our disk space output. Similarly, you can specify the drive that you want to check its disk space using its type. First, check the disk space using the -T option to display their file system type.

$ df -ht

Let’s specify the ext4 file system type. For that, add the -t option.

$ df -ht ext4

Suppose you want to check the total disk space of all /dev/sda in your system. You could use the –total option and a wildcard to match all the sda file systems.

$ df -h --total /dev/sda*

Now that we checked our available disk space, it’s time to check the disk usage for different files and folders using the du command.

Working with DU

Running the du command displays the directories in your system alongside the space that they are using. Adding the -h displays the disk usage in a human-readable format.

If you want to view the disk usage for all files and folders in the current directory, add the -a option for all.

$ du -a

To view the total disk usage of all directories in the current location without displaying their subdirectories, use the -s option.

$ du -hs

We can choose to return the summary information for all directories by adding the asterisk (*).

$ du -h -s *

The output that we get is not sorted. Combine the du command with sort command to get a sorted disk usage output from the largest to the smallest.

Suppose you want to get the disk usage of a given directory. You can specify its absolute path. Here’s an example:

Conclusion

Checking the disk space in Linux via the command line is possible using the df and du commands. This post presented the two commands and gave different examples on how to use them to check the disk space and the disk usage summary by different files and folders.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.