If running du
on a particular directory takes 20 seconds, then that is what it will take to run du
on that directory. You can't really do it quicker.
What you can do quicker is to eliminate the manual labor, i.e. remove the need to run du
manually on each individual directory and to compare the sizes.
You can do this in a few different ways, depending on how you want to count the sizes. If your subdirectories of your home directory contains hard links (files that have multiple names in the file hierarchy), and you want to count these once per name, you can do this with the -l
("ell", not "one") option to GNU du
. With standard du
, multiple hard links would be counted separately only if encountered in separate du
runs.
You also have to decide whether you want to count the apparent ("logical") size, i.e. the number of bytes that a program reading the file from start to finish would read, or the disk usage ("physical") size, i.e. the number of bytes actually used for storing the file on disk. These two sizes may be different from each other if a file is sparse, i.e. if it contains holes. Unallocated disk images or swap files are typically sparse. GNU du
can give you the "logical" sizes using --apparent-size
or --bytes
, but standard du
can't.
Assuming that you are only interested in actual disk usage of each directory, individually, i.e. that you want to count hard links multiple times only if they are located in different subdirectories under $HOME
, and that you want to count the actual, "physical", size of any sparse files, then you may loop over the directories in your home directory and run du
on each one like so:
for dirpath in "$HOME"/*/; do
du -k -s -- "$dirpath"
done
The pattern "$HOME"/*/
will expand to a list of pathnames of (visible) directories beneath your home directory, and/or to visible symbolic links to these.
This will give you the sizes in kilobytes. Many du
implementations can show you the "human readable", approximate, sizes if you use -h
in place of -k
(see man du
).
You may sort the output of this loop numerically, with the largest directory appearing first, like so:
for dirpath in "$HOME"/*/; do
du -k -s -- "$dirpath"
done | sort -n -r
If you used -h
in place of -k
with du
, you will need to have a sort
that understands "human readable" units. Some sort
implementations do this with their -h
option (see man sort
).
ncdu