Add up a column of numbers at the Unix shell

Viewed 210602

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?

23 Answers

Here goes

cat files.txt | xargs ls -l | cut -c 23-30 | 
  awk '{total = total + $1}END{print total}'

Instead of using cut to get the file size from output of ls -l, you can use directly:

$ cat files.txt | xargs ls -l | awk '{total += $5} END {print "Total:", total, "bytes"}'

Awk interprets "$5" as the fifth column. This is the column from ls -l that gives you the file size.

You can use the following script if you just want to use shell scripting without awk or other interpreters:

#!/bin/bash

total=0

for number in `cat files.txt | xargs ls -l | cut -c 23-30`; do
   let total=$total+$number
done

echo $total
cat files.txt | awk '{ total += $1} END {print total}'

You can use the awk to do the same it even skips the non integers

$ cat files.txt
1
2.3
3.4
ew
1

$ cat files.txt | awk '{ total += $1} END {print total}'
7.7

or you can use ls command and calculate human readable output

$ ls -l | awk '{ sum += $5} END  {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; }'
15.69 Mb

$ ls -l *.txt | awk '{ sum += $5} END  {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; }'
2.10 Mb

I would use "du" instead.

$ cat files.txt | xargs du -c | tail -1
4480    total

If you just want the number:

cat files.txt | xargs du -c | tail -1 | awk '{print $1}'

In ksh:

echo " 0 $(ls -l $(<files.txt) | awk '{print $5}' | tr '\n' '+') 0" | bc
... |xargs|tr \  +|bc
... |paste -sd+ -|bc

The first command is just one symbol longer (note, it must have two spaces after the backslash!), but it handles the cases with empty lines in a column, whereas the second command results in an invalid expression with extra pluses.

E.g.:

echo "2
3
5

" | paste -sd+ -

results in

2+3+5++

which bc cannot handle, whereas

echo "2
3
5

" | xargs | tr \  +

gives a valid expression

 2+3+5 

which can be piped into bc to get the final result

Here's mine

cat files.txt | xargs ls -l | cut -c 23-30 | sed -e :a -e '$!N;s/\n/+/;ta' | bc

Pipe to gawk:

 cat files.txt | xargs ls -l | cut -c 23-30 | gawk 'BEGIN { sum = 0 } // { sum = sum + $0 } END { print sum }'

If you have R, you can use:

> ... | Rscript -e 'print(sum(scan("stdin")));'
Read 4 items
[1] 2232320

Since I'm comfortable with R, I actually have several aliases for things like this so I can use them in bash without having to remember this syntax. For instance:

alias Rsum=$'Rscript -e \'print(sum(scan("stdin")));\''

which let's me do

> ... | Rsum
Read 4 items
[1] 2232320

Inspiration: Is there a way to get the min, max, median, and average of a list of numbers in a single command?

The most popular answer doesn't work right when the start of the pipe can produce 0 lines, because it ends up outputting nothing rather than 0. You can get correct behavior by always adding 0:

... | (cat && echo 0) | paste -sd+ - | bc

The - is not required for paste. The following will do as long as files.txt contains one or more valid file names:

<files.txt xargs stat -c %s | paste -sd+ | bc

cat is not required to insert 0 in case there is no file. Without a pipe, perhaps more convenient in a script, you could use:

(xargs -a files.txt stat -c %s || echo 0) | paste -sd+ | bc
Related