2

I've got a file with 400k+ numbers, each with a filename and its size in separate lines and I need to add them up to get a total.

See: https://superuser.com/questions/195493/unix-recursive-directory-listing-with-full-pathname-of-file-and-filesize

filename1 size1
filename2 size2

Its not going to be a very large number ... < ~50,000,000

They're all integers, no decimal points, none of them > 120

Need to do this on a standard linux command line. I can modify the script used to generate this output, which is:

find full_path_to_your_directory -type f -printf '%p %s\n'
Community
  • 1
  • 1
siliconpi
  • 8,105
  • 18
  • 69
  • 107
  • Sheesh - found http://stackoverflow.com/questions/450799/linux-command-to-sum-integers-one-per-line in the related box on the right _just_ right after posting this...! Looks promising! – siliconpi Oct 04 '10 at 09:50
  • Indeed, I wish the related sidebar could be [incorporated when asking a question](http://meta.stackexchange.com/q/42878/54262). –  Oct 04 '10 at 10:03

3 Answers3

1
find . -type f -printf '%p %s\n'  | awk '{sum+=$NF}END{print sum}'

If you want to use Perl,

find . -type f -printf '%p %s\n' | perl -ane '$sum+=$F[1];END{print "$sum\n"}'
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

I got this :

find . -type f -printf '%p %s\n' | perl -n -a -e '$sum+=$S[1]; print "$sum\n"'

which displays the running total.

find . -type f -printf '%p %s\n' | perl -n -a -e '$sum+=$F[1]; print "$sum\n"' | tail -n 1

will just show the total.

With awk it is slightly more compact :

find . -type f -printf '%p %s\n' | awk '{ sum+=$2}; END { print sum}'
Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
0

Since you don't need the filename to sum the sizes:

find path -type f -printf '%s\n' | awk '{sum += $1} END {print sum}'