Earlier today I was working with a large group of files where I needed to estimate the growth over time. In these scenarios, it’s useful to know the average size of the files in a collection. This bash one-liner does the trick.

ls -l | awk '{s+=$5} END {print "Average file size: " s/NR/1024 "k"}'
^[1]    ^[2] ^[3]    ^[4]                             ^[5]

  1. ls -l prints a list of files in long format.

  2. Awk is like a swiss army knife for processing text in regular formats (comma, space, tab, separated content).

  3. Awk uses blocks are similar to Ruby’s. A block {} without any label is executed for each line in the input. Awk syntax supports most common operaters, so the expression s+=$5 works a lot like you’d think it would. ‘s’ is just a variable (implicitly declared) to store a sum. The dollar sign is used to reference “fields”; $5 is the fifth field in a separated (space is the default) list. This keeps a running total of the fifth field in each record.

  4. The END {} block is executed when the EOF is reached. The call to print() outputs to the stdout.

  5. String concatenation in awk is automatic, so you can just enter/exit quotes at will and awk print() will concat the output.