Finding the total size of a set of files with awk

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 CharlyBr, posted on April 17, 2009 at 11:12 am, filed under Command line and tagged . Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Timeline

1 Comment

Have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

:

: