The df and du commands are two small utilities that are extremely useful. The df command displays the disk free space and the du command displays the disk usage. There basic usage is df [options] and du [options].

Using du

The basic usage of du is simply du. When run you will see an output like this:

root@ubuntu:~# du
28      ./scripts
4       ./.cache
8       ./.ssh
16      ./.aptitude
108     .

This means that this directory is using 108Kbytes of disk space. This is actual disk space used not the size of the files. So a 1 byte file would show as using a whole unit of disk storage which on the test system this tutorial is using equals 4K.

There are a number of useful options for du. The main ones are -a, -c and -h. The -a option shows all files as well as folders in the list. The -c option provides a total and the -h option gives the values in a human readable form. The human readable form gives the entries with the best suffix for readability. For instance instead of 86604 it will give you 85M.

Using df

Using df on its own will give you an output like this:

root@ubuntu:/var/log# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       20511356 1982284  17464116  11% /
none                   4       0         4   0% /sys/fs/cgroup
udev              240040       8    240032   1% /dev
tmpfs              50180     348     49832   1% /run
none                5120       0      5120   0% /run/lock
none              250896       0    250896   0% /run/shm
none              102400       0    102400   0% /run/user

The important line in this out put is /dev/vda1, as this is our main mounted filing system. You may also see /dev/sda1, /dev/hda1 plus others. You can safely ignore any line that has none in the first column. You can also safely ignore tmpfs unless you are having specific problems with memory mounted temporary filing systems. You can also ignore udev unless you are specifically having mounting issues. That just leaves us with our main line of interest.

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       20511356 1982284  17464116  11% /

There is only one option that you will find useful in day to day uses and that is the -h option. This does the same as it does for du in that the output will be in human readable units. If you execute df -h on the same system you will see this.

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  1.9G   17G  11% /

So rather than 20511356 1982284 17464116 you get 20G 1.9G 17G which is much more useful at first glance.

Useful tricks

If you are using df and du, it is likely that you have a file system issue and the most common one will be a partition being full and you want to track down where….

This command will come in handy.

du -Sh | sort -rh | head -n 5

What this does is pipe the output from du with the human readable option and the ignore subdirectory option to sort. The sort command will then sort the lines by size (human readable) and reverse the list. The output of this is then sent to head which will just give you the first -n lines. In this case it is 5 lines. This will give you the top 5 folders by size in the current directory. You can then cd into a directory and run it again…