Linux Commands for checking how much space is left on disk

Linux Commands for checking how much space is left on disk

As Linux users, we often continuously save documents and media on our machines but have no idea how to check how much more space is available on our disk, especially if it's a Linux environment without a GUI.

With a distribution like Ubuntu, Linux-mint, and the likes, you can easily go to the "disks" application and view all the disks attached to your computer and the space available, but if you are a command-line lover or you want a fine-grained display of the available space on the terminal, We will show practical examples, using two basic commands to check the space available. It doesn't matter what Linux distribution you've got, the same command works across board for all Linux machines.

The DF command

The df (disk free) command is used to check the free space on a disk. It provides a comprehensive result which contains the used and available space, size, mount path and so on.

Screenshot 2022-01-13 at 6.08.22 PM.png

To show content in a human readable format, we add the h flag to the command

$ df -h

To narrow down the content to a particular disk, let's assume the disk is /dev/sda5

$ df -h /dev/sda5

The result is split in columns with headers which can also help narrow down our search with key words like size, source as shown in the image below:

$ df -h --output=source,size

Screenshot 2022-01-13 at 6.07.01 PM.png

To exclude or completely remove a particular file system from the result,

df -x /dev/sda1

To get a total sum of the entire drives on the machine, add the * - - total flag* to the command Get a summary with that - -total flag

To determine type of file system run: df -T

Screenshot 2022-01-17 at 2.33.57 AM.png

The du command

This command is used for directory analysis. It shows the space consumed by directories recursively. This can help us analyze disk usage by directory and to know what directories are using up the most data.

When we pass the -h flag( Human readable format), it attaches a unit to the numbers on the first column unlike when we run du without any unit.

Screenshot 2022-01-17 at 2.02.53 AM.png

To view all directories, including all hidden folders run du -ah

image.png

We can also add the -c or --total flag as in above to get the sum of all the directory sizes, you can also pass the -s flag as in du -sh to get a summary of the total space of all directories or disk used.

To check and summarize the size of a specific directory:

du -hs /baeldung/checkdiskspace

You can also check baeldung's archives for more information on how to run these commands.