[TriLUG] [bash] get total size of list of directories?

Steve Thorpe steve_thorpe at acm.org
Sun Apr 13 19:31:13 EDT 2008


Probably there is a much simpler way.... but here's an example of how 
one might find the individual sizes of 3 dirs, then below that how one 
could find just the sum of them.  Note each of these examples is all on 
one line although it may spill over in the email message.

24:/usr/local root# du -sk info lib man | cut -f1
572
230204
2056

24:/usr/local root# total=0; for dir in info lib man; do size=`du -sk 
$dir | cut -f1`; total=`echo $total + $size | bc`; done; echo $total
232832


HTH,

Steve

Tom Roche wrote:
> Can one get the total size of a list of directories via shell
> scripting? I know I can do something like
> 
> $ find /this -maxdepth 1 -type d | grep -ve 
> '/this$\|norThat$\|norTheOther$' | xargs du -hs
> 
> but that just gives me the size of each dir in the list, and
> 
> $ find /this -maxdepth 1 -type d | grep -ve 
> 'this$\|norThat$\|norTheOther' | du -hs
> 
> fails oddly: value reported is wildly low, so I suspect it's just
> getting one directory then returning. How to get the sum of all?
> 
> TIA, Tom Roche <Tom_Roche at pobox.com> first trilug post from my eee!


-- 
steve_thorpe at acm.org



More information about the TriLUG mailing list