Useful Linux commands: How to use df and du

man page: man df/du

This post describes how to use the Linux commands df (disk free) and du (disk usage) which show details about the free space on mounted drives as well as usage details. This is useful in many situations, especially when you are fighting against space problems on a server.

Checkout also the general overview post: Useful Linux commands

Summary

CommandExplanation
dfshow mounted drives and their free space
dushow disk usage for current directory and all sub-directories
[df/du] -hcommand plus show bytes in human readable form, e.g. in M for Megabyte
[df/du] -ashow all entries (remote, duplicate..) for df, include files for du
[df/du] -lshow only local entries
[df/du] --totalproduces a total row at the end to show overall statistics
du -sshow only the total for the directory

Usage of df

Simply run command df (down below with -h option for human readable sizes in output) to see mounted drives, the mount location the total size and the used size.

~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        59G   15G   43G  26% /
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           769M  1.8M  768M   1% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/mmcblk0p1  253M   31M  222M  13% /boot

To get a total statistics at the end of the output just add a --total

~ $ df -h --total
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        59G   15G   43G  26% /
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           769M  1.8M  768M   1% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/mmcblk0p1  253M   31M  222M  13% /boot
total            64G  6.4G   56G  11% -

Usage of du

The du command shows the disk usage of a specific directory and all sub directories. If no directory is provided in input it relates the output to the current directly. The next example shows the backup folder of this block with the most recent backups.

~/wordpressbackup $ du -h
48M	./20230101_010001
40M	./20230122_010001
40M	./20230108_010001
39M	./20230115_010001
165M	.

The command can be executed also for specific directories.


When du is executed with the -a option also the sizes of the files inside the folders are shown.

~/wordpressbackup $ du -ah
47M	./20230101_010001/wordpress-filedir.tar.gz
984K	./20230101_010001/wordpress-db.sql
48M	./20230101_010001
39M	./20230122_010001/wordpress-filedir.tar.gz
1.2M	./20230122_010001/wordpress-db.sql
40M	./20230122_010001
38M	./20230108_010001/wordpress-filedir.tar.gz
2.2M	./20230108_010001/wordpress-db.sql
40M	./20230108_010001
38M	./20230115_010001/wordpress-filedir.tar.gz
1.1M	./20230115_010001/wordpress-db.sql
39M	./20230115_010001
165M	.

if only the size of the directory and all the subdirectories are relevant the -s option can be used.

~/wordpressbackup $ du -sh
165M	.

Leave a Reply

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