du -s *.ext (total)

How would I get a total of a wildcard of files from the 'du' command?

du -s *.pdf

Result:

736	11mens_bracket.pdf
64	2011_NIT_Bracket_3_13_11.pdf
80	Doing more with Less part1.pdf
1064	Doing more with Less part2.pdf
28	Parkview_1309_Garage.pdf
3964	statsheet-bracket-2011.pdf

Expected result:
'736+64+80+1064+28+3964'

5936
$ du -s *.* | ruby -ane 'BEGIN{s=0};s+=$F[0].to_i;END{puts "Total: #{s}"}'
du -s *.pdf |awk '{sum+=$1}END{print sum}'
1 Like

I didn't have ruby installed by default, I installed ruby, but that command produced an error I don't understand.

-e:1: undefined method `+' for nil:NilClass (NoMethodError)

EDIT: @rdcwayx, Thanks, that works perfectly! And, I know bash better than ruby, (perl, python, etc.)

du -c *.pdf

@ mirni:
That works too, maybe with the 'tail' command, but I am getting tired and both results do not fit into my function, (even though both work from the cli) check back in the morning.

Thanks!!!

du -c *.pdf | tail -1 | awk '{print $1}'

would print just the number

I have GNU tail version 8.4 so I changed it like this

du -c *.pdf | tail -n 1 | awk '{print $1}'