2
Oct 08

Calculating total file size with awk

Imagine you want to calculate the total size for all of your doc files. Here is a simple way to do it:

find -name '*.doc' -exec ls -al {} \; |awk '{SUM += $5} END {print SUM}'

What it does is find all the doc files, then pass them to the ls -al command through the exec option. Lastly awk takes the 5 column (the column for the file sizes), adds all the values and prints the result.