If you need to sum the total size of files in a directory or matching a pattern, an easy solution is to use awk.
I needed to calculate this total for a set of javascript files, I used this command line:
$ find App/ -name '*.js' -exec ls -l \{\} \; | awk '{sum+=$5} END {print sum}'
1929403
For a human readable result, you can divide your result and use printf to format it:
$ find App/ -name '*.js' -exec ls -l \{\} \; | awk '{sum+=$5} END {printf("%.2fM\n", sum/1024/1024)}'
1.84M
This entry was written by , posted on April 17, 2009 at 11:12 am, filed under Command line and tagged awk. Leave a comment or view the discussion at the permalink.