End users and system administrators sometimes need to get accurate figures for disk usage, in a folder (directory) or file. The du command can help with this. It is used to check disk space, and is one of the most useful commands for reporting disk usage. This utility is shipped with the Coreutils package and is included by default in Fedora.
You can list the file size like this:
$ du anaconda-ks.cfg 4 anaconda-ks.cfg
The -h switch changes the output to use human readable numbers:
$ du -h anaconda-ks.cfg 4.0K anaconda-ks.cfg
In most cases, your goal is to find the disk space usage in a folder or its contents. Be aware that this command has file and folder permissions that apply to this content. Thus, if you are working with system folders, you should probably use the sudo command to avoid permission errors.
This example lists the contents and their sizes in the root folder (/):
sudo du -shxc /*
Here’s what the options are:
- -s = summarize
- -h = human readable
- -x = one filesystem – don’t look at directories on different partitions. For example, on most systems, this command mostly ignores the contents of / dev, / proc, and / sys.
- -c = Grand Total
You can also use -exclude, the ability to ignore the disk usage of a specific directory:
sudo du -shxc /* --exclude=proc
You can provide a file extension to exclude, such as .iso, .txt, or * .pdf. In addition, you can exclude all folders and their contents:
sudo du -sh --exclude=*.iso
You can also limit the depth of the directory structure with –max-depth. You can only print the total for a directory (or file, with -all) if N or fewer levels are below the command line argument. If you use –max-depth = 0, you will get the same result as with the –s option.
sudo du /home/ -hc --max-depth=2