How To Check Disk Space in Linux: Fast and Easy Ways

checking disk space in linux

Whether you’ve never used Linux servers or switched to one from a Windows server, you might want to know how much free space you have on your drive. 

The nice thing about Linux is that it allows you to find such details quickly with a terminal. In this guide, we’ll see how you can use two commands to accomplish this.  

Checking Disk Space with the df Command

The syntax of the df command is as follows:

df [options] [devices]

Of course, both [options] and [devices] are optional. You can simply run df to see the number of used and available “1k-blocks” on all the filesystems associated with your machine. 

You will also see where the filesystems are mounted and the percentage of disk space used. The output you see may also have data other than this. 

Here are brief explanations of all the columns you will see in the output:

  • Filesystems: These are the names of your machine’s drives, whether physical or logical. 
  • Size: This is the total space the drive offers. 
  • Used: This column indicates the space used on the filesystem. 
  • Avail: Under this column, you will see the free space available on every filesystem. 
  • Use%: This column is where you’ll find the percentage of space used. 
  • Mounted on: The data in this column indicates the mount points or the directories where the filesystems are located. 

Some of the entries you may find under the filesystem column may include the following:

  • /dev/sda2: The “/dev” part of this entry means device. The “sda2” part indicates that it is a physical drive. You might not see “sda2,” but rather “sda0” and “sda1.”
  • udev: This entry is a virtual directory that the /dev directory uses and is part of the operating system.
  • tmpfs: This is a temporary filesystem that the operating system uses to function. There can be many such entries, all of which are used by /run the various Linux processes on your machine. 

You can use the df command in several ways.

If you want to see the disk space information in human-readable format instead of 1K blocks, you can run df with the -h option.  

But note that this option will be flexible with the output, showing you the sizes in kilobytes, megabytes, and gigabytes depending on the file.

That said, you can get the size details of every location in kilobytes or megabytes. You can use the -k option to get the size in kilobytes and the -m option to get it in megabytes. 

You might need the information of a specific filesystem, and the df command makes doing this easy. All you’ll need to do is supply a mount point or device as an argument. But this only works if the filesystem is physically on the machine. 

The commands below give you the details about the /dev/sda partition:

df /dev/sda

df -h /dev/sdc1

df /data/

The result of these commands will show you the total, used, and available 1K blocks. 

What’s interesting is that you can also check an NVME disk’s space with this command. You’ll need to use the command, preferably with the -h option, and the location you want to check. 

You can use the –output option with the corresponding field name if you need a detailed description of a specific column rather than an overview. 

The valid field names are as follows:

Display Names Valid Field Name
Filesystem source
1K-blocks size
Used used
Available avail
Use% pcent
Mounted on target

The commands you must write look like this:

df –output=source,used,avail /data/

df –output=field1,field2,…

To print all the “available” fields that the df command can gather and offer, you can run the following command:

df –o

The df command will output the disk space details according to the inode usage instead of the block usage if you use the -i option. Inodes are data structures responsible for storing file information. 

In some cases, the type of every filesystem might be relevant to you. To determine whether the filesystems associated with your machine are btrfs, ext2, ext4, fuse, nfs4, cgroup, and so on, use the -T option. The output table will now include a column showing you the type. 

You can also mention a device when using this option with the command if you prefer.  

Conveniently, the df command also enables you to find whether there are filesystems of a specific type associated with your machine. If you run the command with the -t option and also pass the type of filesystem you’re looking for, the command will print the relevant details.

Here’s what your command would look like with this option:

df -t ext3

Perhaps more interestingly, it’s possible to exclude filesystems of specific types when checking the disk space with the -x option. So, if you don’t want to see the details of any ext2 filesystems associated with your machine, you could run the following:

df -x ext2

To include all filesystems associated with your machine in your output, use the -a option. 

Checking Disk Space with the du Command

The du command is tailored to help users find directories and files that hog up the most disk space. The syntax looks like this:

du [options] [directories and files]

As you might be able to tell, using options and mentioning directories and/or files with this command is not strictly necessary. 

If you run “du” without any options or arguments, you will see the space consumption and names of every directory, including the subdirectories, in that order.

The du command, like the df command, supports the “-h” option. It serves the same function, showing you the total size, available space, and used space in a human-readable format.  

If you want the du command to only show you the total disk space that a directory tree occupies and aren’t interested in the subdirectories, you can use the -s option. 

Say you want to see the total disk space in /etc/, you would run:

sudo du -s /etc/

In contrast, you might want to use the -a option to see all the files rather than the directories. Just like the command above, you will need to pass the location you’re interested into this command:

du -a /etc/

Using the star wildcard, you can zone into finding the biggest space-hoggers of a specific type of file. The * will match any character, so let’s say you want to check the size of the PNG files in your current directory. You could run: 

du -ch *.png

Note that the -c option instructs the du command to display the grand total in terms of size.

Conclusion

Now that you have this guide handy, you should have no problem figuring out the available, used, and total disk space of filesystems and directories using the df and du commands. 

You can learn more about the various options and arguments available to you using the –help command with both commands. 

For more information, click here