How to Check Linux Disk Space

By | 2019-04-17

Checking disk space in Linux can done with the command line progams df and du. df is used to show usage at the filesystem level, while du can be used to show how much space individual files and directories use.

If you aren’t familiar with how Linux presents storage devices, you will probably want to read the mounting section of: Linux Filesystem Hierarchy.

Checking Disk Usage With df

The command line program df is used to show filesystem usage information. Here is an example:

[root@centos7 ~]# df 
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root   6981632 2505032   4476600  36% /
devtmpfs                   929916       0    929916   0% /dev
tmpfs                      941312       0    941312   0% /dev/shm
tmpfs                      941312    8664    932648   1% /run
tmpfs                      941312       0    941312   0% /sys/fs/cgroup
/dev/sda1                  508588  293572    215016  58% /boot
tmpfs                      188264       0    188264   0% /run/user/0

With no arguments df displays all mounted filesystems’ usage statistics in 1K blocks. In this context, 1K is 1024.

Common df Options

Here are a few common options for df.

Option Description
-h Display the disk usage in human readable sizes using an appropriate power of 1024.
-H Display the disk usage in human readable sizes using an appropriate power of 1000.
-k Display the disk usage in KiB
–total Displays a totals line at the bottom of the output.

Checking Usage of a Particular Filesystem

If you wish to see how much space is available to a particular file or directory, you can pass it to df as a command line argument. When you do this, df determines which device the file or directory is stored on and displays its usage stats.

[root@centos7 ~]# df -h /boot/grub
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       497M  287M  210M  58% /boot

Checking File and Directory Disk Usage Withdu

Sometimes you may want to check how much space a file or directory is using. ls can do it, but du is much easier in many cases. Also, ls won’t show you recursively much space a directory with subdirectories is using.

The example below demonstrates checking the usage of the current directory:

[root@centos7 sysconfig]# pwd
/etc/sysconfig
[root@centos7 sysconfig]# du
8	./cbq
0	./console
0	./modules
224	./network-scripts
316	.

By default, du recursively outputs the disk usage of your current directory and all of its subdirectories in 1K blocks. As with df, du has a -h option that will display the statistics in human-readable powers of 1024:

[root@centos7 sysconfig]# pwd
/etc/sysconfig
[root@centos7 sysconfig]# du -h
8.0K	./cbq
0	./console
0	./modules
224K	./network-scripts
316K	.

Summarizing Output

The output of du can get large. If you only care about the total usage of the current directory, you can suppress the output with -s.

[root@centos7 bin]# pwd
/usr/bin
[root@centos7 usr]# du -hs
1.1G	.

Specifying Files and Directories

You can specify files and directories by naming them on the command line:

[root@centos7 etc]# du /etc/ssh /etc/sysconfig /etc/hosts
608	/etc/ssh
8	/etc/sysconfig/cbq
0	/etc/sysconfig/console
0	/etc/sysconfig/modules
224	/etc/sysconfig/network-scripts
316	/etc/sysconfig
4	/etc/hosts

Combining df and du to Troubleshoot Problems

I commonly use these two tools to find the source of a disk that is filling up. The following example shows a realistic scenario where the yum cache on a CentOS host is filling up the disk.

[root@centos7 /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  6.7G  5.4G  1.4G  80% /
devtmpfs                 909M     0  909M   0% /dev
tmpfs                    920M     0  920M   0% /dev/shm
tmpfs                    920M  8.5M  911M   1% /run
tmpfs                    920M     0  920M   0% /sys/fs/cgroup
/dev/sda1                497M  287M  210M  58% /boot
tmpfs                    184M     0  184M   0% /run/user/0
[root@centos7 /]# cd /
[root@centos7 /]# du -hs *
0	bin
500M	blah
262M	boot
0	dev
35M	etc
68K	home
112K	ldapdata
0	lib
0	lib64
0	media
0	mnt
21M	opt
du: cannot access ‘proc/4986/task/4986/fd/4’: No such file or directory
du: cannot access ‘proc/4986/task/4986/fdinfo/4’: No such file or directory
du: cannot access ‘proc/4986/fd/4’: No such file or directory
du: cannot access ‘proc/4986/fdinfo/4’: No such file or directory
0	proc
589M	root
8.5M	run
0	sbin
0	srv
0	sys
0	tmp
1.1G	usr
3.2G	var
[root@centos7 /]# cd var/
[root@centos7 var]# du -hs *
0	adm
3.0G	cache
0	crash
8.0K	db
0	empty
0	games
0	gopher
0	kerberos
124M	lib
0	local
0	lock
4.6M	log
0	mail
0	nis
0	opt
0	preserve
0	run
24K	spool
0	tmp
0	yp
[root@centos7 var]# cd cache/
[root@centos7 cache]# du -hs *
20K	ldconfig
1012K	man
3.0G	yum
[root@centos7 cache]# cd yum
[root@centos7 yum]# du -hs *
3.0G	x86_64
[root@centos7 yum]# cd x86_64/
[root@centos7 x86_64]# du -hs *
3.0G	7
[root@centos7 x86_64]# cd 7
[root@centos7 7]# du -hs *
36M	base
8.6M	centosplus
1.2M	extras
4.0K	timedhosts
4.0K	timedhosts.txt
3.0G	tylersguides
21M	updates
[root@centos7 7]# rm -rf tylersguides
[root@centos7 7]# df -h /
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  6.7G  2.4G  4.3G  36% /

References

  • The df man page
  • The du man page