This is a two step process. First find the disk usage of every file and then calculate the values.
For the first du
is clearly my favorite.
find /usr/bin -type f -exec du '{}' '+'
This will search ever file (-type f
) and will append ('+'
) its filename ('{}'
) to an invokation (-exec
) of du
.
The result will be a tab separated list of usage (in blocks IIRC) and filename.
Now comes the second part (here for the average). This list are we going to feed into awk
and let it sum up and divide by the number of rows
{ sum = $1 } END { print "avg: " sum/NR }
The first block is going to be executed every line and will add the value of the first (tab separated) column to the variable sum
. The other block is prefixed with END
meaning that it will get executed when the stdin is EOF. NR
is a special variable meaning the number of rows.
So the finished command looks like:
find /usr/bin -type f -exec du '{}' '+' | awk '{ sum += $1 } END { print "Avg: " sum/NR }'
Now go read about find
, awk
and shell pipelines. Those things will make your life considerably easier when you have to deal with linux shell stuff. Also basic knowledge about line buffering and standard IO streams is helpful.