Total size of multiple directories

Andrew Hunter afrogofmyown at gmail.com
Fri Jun 25 14:19:26 MDT 2010


>     I'm looking for a way in bash to display the sum total size of multiple
> directories.  Think 'du -sh', but only include certain directories.
>  Ideally, I'm looking for something I can use with 'find' to get a quick
> idea of how much space a group of directories is using.  Any ideas?

The way I typically use is a little brute-force-ish and inelegant in
nature.  I use 'du -d1' (or, probably more common, 'du --max-depth=0'
and sum the results.  So, for example:

du -d0 dir1 dir2 dir6 dir8 dir11 dir15 dir21 | awk '{sum = sum + $1 ;
print} END {print sum}'

(or)

du --max-depth=1 dir1 dir2 dir6 dir8 dir11 dir15 dir21 | awk '{sum =
sum + $1 ; print} END {print sum}'

2940972 dir1
1043935 dir2
1365310 dir6
317     dir8
610     dir11
5448534 dir15
1558372 dir21
12358050

The last line, of course, is the sum of all dirs.  Sadly, this does
not preserve the nice human-friendly output of the -h.  I hope that's
an acceptable loss...

Andrew


More information about the PLUG mailing list