How to check disk space with df and du on Linux

Df and du are two very useful utilities which are normally installed by default in all Linux distributions. We can use the first one to obtain an overview of the used and available space on mounted filesystems; the second, instead, is very useful to obtain a detailed report about the space used by files and directories. In this article we take a look at their usage, and we see what are the most common used options which can be used to modify their behavior.

In this tutorial you will learn:

  • How the df utility works
  • How to show the output of df in human-readable form
  • How to include the filesystem type in the output of df
  • How to include or exclude filesystems from the output of df
  • How the du utility works
  • How to obtain a human-friendly output with du
  • How to obtain a summary of the used space
  • How to exclude files from the output of du
  • How to obtain a “grand total” of the space in use by multiple directories

How to check disk space with df and du on Linux

How to check disk space with df and du on Linux

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software df and du (installed by default)
Other No other requirements needed
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Df

The df utility is used to display, among the other things, filesystems available and used disk space. If the program is called without any argument, all mounted filesystem are included in the report:

$ df
Filesystem                       1K-blocks     Used Available Use% Mounted on
devtmpfs                           2908316        0   2908316   0% /dev
tmpfs                              2930156    28392   2901764   1% /dev/shm
tmpfs                              2930156     1792   2928364   1% /run
tmpfs                              2930156        0   2930156   0% /sys/fs/cgroup
/dev/mapper/fingolfin_vg-root_lv  35862048  7210616  26800040  22% /
tmpfs                              2930156      124   2930032   1% /tmp
/dev/sda1                          1032088   161664    817996  17% /boot
/dev/mapper/fingolfin_vg-home_lv  25671908  1515396  22829408   7% /home
/dev/mapper/fingolfin_vg-data_lv 152737296 90208196  54700828  63% /mnt/data
/dev/dm-5                        152786272 90208644  54746804  63% /mnt/databk
tmpfs                               586028     1124    584904   1% /run/user/1000

Let’s take a look at the output of the command. In the first column we have the filesystem path, in the second we have its size, which is reported in 1K blocks. In the third and fourth columns we have the used and available space respectively. In the fifth column the usage percentage is reported, while in the last column we can see the filesystem mountpoint.

Df accepts one or more file paths as arguments. When those arguments are provided only the information about the filesystem on which the files reside will be reported. For example, if we specify /home/egdoc/.bashrc as file argument, we will get a report about the filesystem mounted on /home, which, in this case, is on a separate LVM logical volume:

$ df /home/egdoc/.bashrc
Filesystem                       1K-blocks    Used Available Use% Mounted on
/dev/mapper/fingolfin_vg-home_lv  25671908 1515468  22829336   7% /home

If we pass a mountpoint as argument to the command, information about the mounted filesystem are reported instead.

Displaying information in human-readable form

As you can see in the output of the previous examples, filesystem sizes and used space are reported in 1K-blocks, so they are not very human friendly. To get those information in a format more understandable by us humans, we can invoke df with the -h option, which is the short for --human-readable. When the option is provided, the sizes are reported in power of 1024, with a convenient suffix:

$ df -h /home/egdoc/.bashrc
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/fingolfin_vg-home_lv   25G  1.5G   22G   7% /home


Including or excluding filesystems

In some cases we may want only some specific filesystems to be included in the output of the command. We will see how to achieve this task in a moment, but first let’s see how to include the filesystem type in the output of df. The -T
(--print-type) option let us achieve exactly that:

$ df -h -T
Filesystem                       Type      Size  Used Avail Use% Mounted on
devtmpfs                         devtmpfs  2.8G     0  2.8G   0% /dev
tmpfs                            tmpfs     2.8G   22M  2.8G   1% /dev/shm
tmpfs                            tmpfs     2.8G  1.8M  2.8G   1% /run
tmpfs                            tmpfs     2.8G     0  2.8G   0% /sys/fs/cgroup
/dev/mapper/fingolfin_vg-root_lv ext4       35G  6.9G   26G  22% /
tmpfs                            tmpfs     2.8G  124K  2.8G   1% /tmp
/dev/sda1                        ext2     1008M  158M  799M  17% /boot
/dev/mapper/fingolfin_vg-home_lv ext4       25G  1.5G   22G   7% /home
/dev/mapper/fingolfin_vg-data_lv ext4      146G   87G   53G  63% /mnt/data
/dev/dm-5                        ext4      146G   87G   53G  63% /mnt/databk
tmpfs                            tmpfs     573M  1.2M  572M   1% /run/user/1000

To exclude or include certain filesystem types from the output of the command, we can basically use two options: -t (--type) and -x (--exclude-type). The option names are pretty self explanatory: we can use the former to specify the
filesystem type which should be included in the output. For example, to get information only about ext4 filesystems, we would run:

$ df -h -t ext4
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/fingolfin_vg-root_lv   35G  6.9G   26G  22% /
/dev/mapper/fingolfin_vg-home_lv   25G  1.5G   22G   7% /home
/dev/mapper/fingolfin_vg-data_lv  146G   87G   53G  63% /mnt/data
/dev/dm-5                         146G   87G   53G  63% /mnt/databk

The latter option, -x, let us specify the filesystem type which should be excluded from the output of df, instead. To display all filesystems except the ext4 ones, we would run:

$ df -h --x ext4
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        2.8G     0  2.8G   0% /dev
tmpfs           2.8G   22M  2.8G   1% /dev/shm
tmpfs           2.8G  1.8M  2.8G   1% /run
tmpfs           2.8G     0  2.8G   0% /sys/fs/cgroup
tmpfs           2.8G  124K  2.8G   1% /tmp
/dev/sda1      1008M  158M  799M  17% /boot
tmpfs           573M  1.2M  572M   1% /run/user/1000q

Du

The du utility is used to report directories and file space usage. When invoked with no arguments, the utility (recursively) displays the list of all the directories existing in the current position, together with their space usage (the output below is truncated for convenience):

$ du
4   ./Public
[...]
4   ./Documents
4   ./Videos
4   ./Pictures
1469376 .

Just like in the case of df, we can make the du command produce a more human-readable output by using the -h option, short for --human-readable. When the option is used, for example, the output above becomes:

$ du -h
4.0K    ./Public
[...]
4.0K    ./Documents
4.0K    ./Videos
4.0K    ./Pictures
1.5G    .

In case we want to visualize only the summary of the used disk space in a certain directory, we can use the -s option, which is the short for --summarize. If we invoke the command with this option, in the same working directory as before, we obtain the following result:

$ du -hs
1.5G    .


Including files and their size in the output of du

By default, as we can observe, only directories are reported in the output of the du command. To include also standard files in the output, we can use the -a option (short for -all). If we re-launch the command with this option we obtain the following:

$ du -ha
4.0K    ./Public
4.0K    ./.Xauthority
4.0K    ./.bashrc
[...]
4.0K    ./Documents
4.0K    ./Videos
4.0K    ./Pictures
1.5G    .

As you can see, the .Xauthority and .bashrc files (and the respective space usage), were included in the (truncated) output.

Excluding files matching a pattern

We have basically two ways to specify the files and directories which should be excluded from the output of the du command: the first one is by invoking the program with the --exclude option. This option takes a pattern as argument; all the files and directory names matching that pattern will be excluded. Just as an example, suppose we want to exclude all hidden files from the output of du. We know hidden files are named starting with a dot, therefore we could run:

$ du -ha --exclude=./.*
4.0K    ./Public
4.0K    ./Music
4.0K    ./Downloads
4.0K    ./Desktop
4.0K    ./Templates
4.0K    ./Documents
4.0K    ./Videos
4.0K    ./Pictures
36K .

As expected, only non-hidden files and directories were included in the output. We can specify multiple exclusion patterns by repeating the --exclude option. For example, suppose we want to also exclude files and directories which
have a name starting with the D character. We would run:

$ du -ha --exclude=./.* --exclude=./D*
4.0K    ./Public
4.0K    ./Music
4.0K    ./Templates
4.0K    ./Videos
4.0K    ./Pictures
24K .

The second way we can use to specify pattern that should be used for file and directory exclusions, is to use the -X option (--exclude-from). This option takes the name of a file as argument: is in that file that we specify the patterns to be matched. Let’s create it. We will call it exclusions.txt:

$ $ cat << EOF > exclusions.txt
> ./.*
> ./D*
> EOF


With our file in place we can run:

$ du -ha --exclude-from=exclusions.txt
4.0K    ./Public
4.0K    ./Music
4.0K    ./exclusions.txt
4.0K    ./Templates
4.0K    ./Videos
4.0K    ./Pictures
28K .

As you can see we obtained the same result as above, except for the fact that the size of the exclusions file (exclusions.txt) is now included in the count of the space used by the current directory (the last line of the output: 24K vs 28K).

Obtaining a “grand total” of the used space

The du utility accepts multiple files and directories as its arguments. For example we can use it to obtain a summary of the space used by the /boot and /etc directories:

$ sudo du -hs /etc /boot
28M /etc
157M    /boot

In the example above we ran du with sudo in order to grant it access to some restricted directories. As you can see the summary of the space usage is reported for each directory we specified. What if we want to obtain a “grand total”, so to have the sum of the space used by the two directories? To achieve that we can use the -c option (short for --total). Our command becomes:

$ sudo du -hsc /etc /boot
28M /etc
157M    /boot
184M    total

Conclusions

In this article we learned to use df and du, two utilities installed by default in all Linux distributions. They are very useful to check the available and used space in mounted filesystems, and to obtain detailed information about the size of file and directories. We covered the most common use cases; for more detailed information, as always, check the manuals!



Comments and Discussions
Linux Forum