Alex Rivera | Logout

Add up a column of numbers at the Unix shell

Asked 2009-05-29T13:50:41.453
243

Given a list of files in files.txt, I can get a list of their sizes like this:

cat files.txt | xargs ls -l | cut -c 23-30

which produces something like this:

  151552
  319488
 1536000
  225280

How can I get the total of all those numbers?

Edit
Report

1 Answer

174

Here goes

cat files.txt | xargs ls -l | cut -c 23-30 | 
  awk '{total += $1}END{print total}'
answered 2009-05-29T14:03:59.167

Your Answer