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
Also, ncdu tool is quite useful, especially to quickly scan for large files/directories on a system.
Agree, nice article, love these small tips.
But I want to bulid on, if you user -d option (max-depth) you can get the size of the folders in the path provided. Greate to find where the files that are taking up space.
“du -s /some/path” is equale to “du -d0 /some/path”
Above will only give you the total space for “/some/path”.
The magic starts to happen when you increase the depth (-d option).
“du -d1 /some/path”
Now you will get the size of each folder in “/some/path” and also the total size. If you continue to increase the depth to 2,3… you will get more info.
Alse throw in a -h so a human can read it, “du -hd1 /some/path” and you will see the magic
I use to pipe to sort in order to find largest dirs:
My favorite is:
du -x /home | sort -rn | less
This could be also combined with -h .
My favorite one-liner is
which makes sure that the bigger directories are listed at the end, making it easier to understand where the space on a specific filesystem (-x) goes lost.
gives the size in Megabytes, making it more readable without losing the nice sorting (which would be the case with -h).
Have you try “du -h –max-depth=1 /root | sort -k 1 -h”?
There is also a nice, however somehow forgotten, tool gt5 – http://gt5.sourceforge.net/ – “based on” du to display a diff of disk usage between two moments in time. Indispensable when working on a server without UI to get know what does progressively eat your precious disk space :).
P.S. I maintain it in Fedora, so “dnf install gt5” is all you need to get started.
Thank you very much. This command is a salvation!