Skip to content
Advertisement

Add up a column of numbers at the Unix shell

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?

Advertisement

Answer

... | paste -sd+ - | bc

is the shortest one I’ve found (from the UNIX Command Line blog).

Edit: added the - argument for portability, thanks @Dogbert and @Owen.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement