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]
-
ls -l
prints a list of files in long format. -
Awk is like a swiss army knife for processing text in regular formats (comma, space, tab, separated content).
-
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 expressions+=$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. -
The
END {}
block is executed when the EOF is reached. The call toprint()
outputs to the stdout. -
String concatenation in awk is automatic, so you can just enter/exit quotes at will and awk
print()
will concat the output.