How to check disk space in Linux

A common problem server administrators face is running out of disk space or even just checking their current disk space usage. In this guide, I’m going to show you how to use the du command to check disk usage and the df command to check disk space.

Check disk space using df command

df stands for disk free and, as you guessed, it will show you the free space on your mounted filesystem. Running

df

will give you:

[root@boltns-fra1-01 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          976836       0    976836   0% /dev
tmpfs            1012324       0   1012324   0% /dev/shm
tmpfs            1012324  105612    906712  11% /run
tmpfs            1012324       0   1012324   0% /sys/fs/cgroup
/dev/vda2       52416492 7982108  44434384  16% /
/dev/loop0       2317152    3656   2192460   1% /tmp
tmpfs             202464       0    202464   0% /run/user/0

This output might be a little confusing at first, but you’ll find it’s actually not that confusing. Filesystem is the name of the filesystem and in this example, my main disk is /dev/vda2 and it’s mounted at / (meaning, this system was installed with a single partition). 1K-Blocks is the size of the filesystem measured in 1KB blocks. Used is the number of 1K blocks being utilized. Available is the amount of space we have free. Use% is the same calculation just in percentage form. Mounted on is where that filesystem is mounted.

1K blocks aren’t very friendly, except for maybe UNIX wizards. So to make this command more human friendly, we run the df command with the -h option. This will give us:

[root@boltns-fra1-01 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        954M     0  954M   0% /dev
tmpfs           989M     0  989M   0% /dev/shm
tmpfs           989M  104M  886M  11% /run
tmpfs           989M     0  989M   0% /sys/fs/cgroup
/dev/vda2        50G  7.7G   43G  16% /
/dev/loop0      2.3G  3.6M  2.1G   1% /tmp
tmpfs           198M     0  198M   0% /run/user/0

Now that should make more sense! /dev/vda2 is 50 gigabytes in size, we’re only using 7.7 gigabytes, leaving us with 43 gigabytes free or only 16% in use.

If you only want to view a specific filesystem (such as / or /home if your system has multiple partitions), you can run df like this: df -h / which will give us:

[root@boltns-fra1-01 ~]# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2        50G  7.7G   43G  16% /

df also comes with other flags to modify the output. You can run df --help to see them all but here are some common ones:

  • -h (human readable, prints sizes in powers of 1024)
  • -H (si, prints sizes in powers of 1000)
  • -m (lists sizes in megabytes)
  • -k (lists sizes in kilobytes)
  • -i (shows inodes instead of blocks)
  • --help (view the help guide)

Finding what’s eating your disk space with du command

If you’re like most Linux admins, you’ve probably ran into a full disk at some point or another… but what’s eating up your disk? On Windows, various third party utilities can show you, but you run Linux! This functionality is built-in with the du command, otherwise known as disk usage.

If you just run

du

you’ll see a rather lengthly output such as this (and I went ahead and truncated this):

[root@boltns-fra1-01 ~]# du
8       ./.ssh
0       ./.gnupg/private-keys-v1.d
112     ./.gnupg
4       ./.HttpRequest
132     ./.MirrorSearch/httpupdate.cpanel.net/pingtimes
136     ./.MirrorSearch/httpupdate.cpanel.net
136     ./.MirrorSearch
272     ./.cpanel/datastore
276     ./.cpanel
4       ./.cpanm/work/1641314316.14682/CPAN-2.29/lib/CPAN/LWP
12      ./.cpanm/work/1641314316.14682/CPAN-2.29/lib/CPAN/Plugin
8       ./.cpanm/work/1641314316.14682/CPAN-2.29/lib/CPAN/Kwalify
[OUTPUT TRUNCATED]
0       ./perl5/lib/perl5/x86_64-linux-thread-multi
0       ./perl5/lib/perl5/5.26.3/x86_64-linux-thread-multi
0       ./perl5/lib/perl5/5.26.3
0       ./perl5/lib/perl5
0       ./perl5/lib
0       ./perl5
0       ./tmp
3768    ./.spamassassin/sa-compile.cache
3768    ./.spamassassin
21080   .

Similar to the df command, you’ll see that the file sizes aren’t human readable. du also has the -h option to show you file sizes in numbers that are easier to read.

[root@boltns-fra1-01 ~]# du -h
8.0K    ./.ssh
0       ./.gnupg/private-keys-v1.d
112K    ./.gnupg
4.0K    ./.HttpRequest
132K    ./.MirrorSearch/httpupdate.cpanel.net/pingtimes
136K    ./.MirrorSearch/httpupdate.cpanel.net
136K    ./.MirrorSearch
272K    ./.cpanel/datastore
276K    ./.cpanel
4.0K    ./.cpanm/work/1641314316.14682/CPAN-2.29/lib/CPAN/LWP
12K     ./.cpanm/work/1641314316.14682/CPAN-2.29/lib/CPAN/Plugin
[OUTPUT TRUNCATED]
680K    ./.cpanm/work/1641314316.14682/YAML-Syck-1.34/blib
2.7M    ./.cpanm/work/1641314316.14682/YAML-Syck-1.34
17M     ./.cpanm/work/1641314316.14682
17M     ./.cpanm/work
17M     ./.cpanm
0       ./perl5/bin
0       ./perl5/lib/perl5/x86_64-linux-thread-multi
0       ./perl5/lib/perl5/5.26.3/x86_64-linux-thread-multi
0       ./perl5/lib/perl5/5.26.3
0       ./perl5/lib/perl5
0       ./perl5/lib
0       ./perl5
0       ./tmp
3.7M    ./.spamassassin/sa-compile.cache
3.7M    ./.spamassassin
21M     .

You can see that it has listed out the sizes of directories based on where we are in the filesystem. For example, the .ssh directory contains 8.0 kilobytes of data in it. The .cpanm directory contains 17 megabytes of data in it. At the bottom, you see that . has 21 megabytes of data in it.

When we troubleshoot our customer’s servers, our most typed du command is du -chx --max-depth=1 / and let’s break this down:

  • -c will produce a grand total
  • -h will make the output human readable
  • -x will only show us the current filesystem
  • --max-depth=1 keeps du from looking in sub-directories.
  • / is where we’re looking, in this case the root

Here’s an example output:

[root@boltns-fra1-01 ~]# du -chx --max-depth=1 /
26M     /etc
21M     /root
1.1G    /var
5.8G    /usr
344M    /boot
152K    /home
0       /media
0       /mnt
0       /opt
0       /srv
0       /backup
7.2G    /
7.2G    total

Using this command and adjusting the path, we can easily narrow down what is using the disk space and (carefully) removing items. For example, this can find log files that aren’t rotating.

du also has a --help option that will show you what else this useful utility can do.

Conclusion

You’ve learned some of the basics of du and df commands and how to find help. You can combine these commands with other commands and find and prevent issues with your disk space. If you need some help or have a combination of flags that work great for you, feel free to let us know in the comments or on our social media!

[porto_block id=”6467″]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.