End users and system administrators sometimes struggle to get exact disk usage numbers by folder (directory) or file. The du command can help. It stands for disk usage, and is one of the most useful commands to report disk usage. This utility ships in the coreutils package included by default in Fedora.
You can list the size of a file:
$ 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 disk usage in and under a folder, or its contents. Keep in mind this command is subject to the file and folder permissions that apply to those contents. So if you’re working with system folders, you should probably use the sudo command to avoid running into permission errors.
This example prints a list of contents and their sizes under the root (/) folder:
sudo du -shxc /*
Here’s what the options represent:
- -s = summarize
- -h = human readable
- -x = one file system — don’t look at directories not on the same partition. For example, on most systems this command will mainly ignore the contents of /dev, /proc, and /sys.
- -c = grand total
You can also use the –exclude option to ignore a particular directory’s disk usage:
sudo du -shxc /* --exclude=proc
You can provide file extensions to exclude, like .iso, .txt, or *.pdf. You can also exclude entire folders and their contents:
sudo du -sh --exclude=*.iso
You can also limit the depth to walk the directory structure using –max-depth. You can print the total for a directory (or file, with –all) only if it is N or fewer levels below the command line argument. If you use –max-depth=0, you’ll get the same result as with the -s option.
sudo du /home/ -hc --max-depth=2