The du
utility is used to estimate disk space usage. Its syntax is:
du [options] [file]
By default, du
recursively lists directories and their size (in kibibytes). Typically, you want to pipe the output to sort
to sort the output by the size:
# du ~example/ | sort -r | head 8348008 /home/example/ 6766096 /home/example/public_html 6698220 /home/example/public_html/wp-content 6574016 /home/example/public_html/wp-content/uploads 2527828 /home/example/public_html/wp-content/uploads/2020 2196120 /home/example/public_html/wp-content/uploads/2019 1798432 /home/example/public_html/wp-content/uploads/2018 1517832 /home/example/mail 1517648 /home/example/mail/example.com 895108 /home/example/mail/example.com/info
And you can add the -h
(--human-readable
) option to print the directory size in units that are easier to digest:
# du -h /home/example/ | sort -hr | head 8.0G /home/example/ 6.5G /home/example/public_html 6.4G /home/example/public_html/wp-content 6.3G /home/example/public_html/wp-content/uploads 2.5G /home/example/public_html/wp-content/uploads/2020 2.1G /home/example/public_html/wp-content/uploads/2019 1.8G /home/example/public_html/wp-content/uploads/2018 1.5G /home/example/mail/sistermintaka.com 1.5G /home/example/mail 875M /home/example/mail/example.com/info
When you look into disk space issues it is usually best to also use the -a
(--all
) option. This prints both directories and files. Any large backup files and/or error logs are included in the output.
And, you can also check the size of a single directory:
# du -sh /home/example 8.0G /home/example
The -c
(--total
) option adds a grand total to the output. You can use that to, say, add up the total disk space usage of two directories:
# du -shc \ /home/example/public_html/wp-content/uploads/2018 \ /home/example/public_html/wp-content/uploads/2019 1.8G /home/example/public_html/wp-content/uploads/2018 2.1G /home/example/public_html/wp-content/uploads/2019 3.9G total
If you run du
on a very large directory then you may want to limit how far the utility traverses into the directory tree. The -d
(--max-depth
) option does just that. For instance, you can use --max-depth=1
to list the size of all directories in /home/example:
# du --max-depth=1 -h ~example| sort -hr | head 8.0G /home/example 6.5G /home/example/public_html 1.5G /home/example/mail 32M /home/example/logs 16M /home/example/tmp 6.5M /home/example/.cagefs 6.4M /home/example/etc 1.6M /home/example/.cphorde 748K /home/example/.cpanel 80K /home/example/ssl
The --exclude
option excludes certain directories. This is particularly useful when you run du
on a server’s root directory. On cPanel servers, for instance, you can safely exclude the following directories:
Excluding these directories makes du
faster and it prevents the output includes junk data.
# du -h / \ --exclude=/proc \ --exclude=/sys \ --exclude=/run \ --exclude=/home/virtfs \ --exclude=/usr/share/cagefs-skeleton/proc \ | sort -hr \ | head -50