I can find the number of lines of each file matching a particular pattern using (for example):

$ find . -name "test.save*" -exec wc -l {} \;
673000 ./test.save8.txt
24000 ./from/test1/test.save3.txt
100 ./from/test1/test.save1.txt
513000 ./from/test1/test.save2.txt
2253000 ./from/test1/test.save4.txt
2252000 ./from/test2/test.save3.txt
100 ./from/test2/test.save1.txt
596000 ./from/test2/test.save2.txt
2224000 ./from/test3/test.save3.txt
100 ./from/test3/test.save1.txt
593000 ./from/test3/test.save2.txt
270000 ./from/test4/test.save3.txt
100 ./from/test4/test.save1.txt
332234 ./from/test4/test.save2.txt
2177000 ./from/test4/test.save4.txt
1728000 ./test.save3.txt
180000 ./test.save1.txt
466000 ./test.save11.txt
233000 ./test.save9.txt
686880 ./test.save5.txt
215262 ./test.save7.txt
2560000 ./test.save12.txt
18080 ./test.save10.txt
432000 ./test.save2.txt
10000 ./test.save4.txt
684000 ./test.save6.txt

How do I add up all the individual numbers (using Linux command-line tools)?


This should do it:

find ... -exec cat {} \; | wc -l

If your version of find supports it, this will be much faster:

find ... -exec cat {} + | wc -l

or if you want both the individual counts and the total:

find ... | xargs wc -l

The first two will handle filenames with spaces. In order to make the last one above work if there are filenames with spaces, use -print0 and -0:

find ... -print0 | xargs -0 wc -l
  • That does not sum up the total number of lines it just prints out what he already has. – topdog Jul 27 '10 at 18:55
  • The second command works great. The first is very slow. (I'm still waiting for it to complete.) Perhaps you should just remove it from your answer. – Daryl Spitzer Jul 27 '10 at 18:58
  • I'm beginning to wonder if the first command works, since I'm still waiting for it to complete. – Daryl Spitzer Jul 27 '10 at 19:35
  • @Daryl: Double check to see if you ran the first version I posted that didn't have the curly braces. Without them, it doesn't work. With them, it does. Also, change the \; to + - it will speed up considerably. I'm editing my answer. – Dennis Williamson Jul 27 '10 at 19:57
  • I was running the version without curly braces. It works with curly braces. The version with + works on Ubuntu 10, but (for me) isn't any faster. – Daryl Spitzer Jul 27 '10 at 20:10

Not very elegant but this would do

for value in $(find . -name "test.save*" -exec wc -l {} \;|awk '{ print $1 }'); do
let total=total+value
echo $total
  • Is this meant to be run as a shell script? Can you add the appropriate shebang? – Daryl Spitzer Jul 27 '10 at 18:55
  • I can't figure out how to execute this. – Daryl Spitzer Jul 27 '10 at 19:02
  • Of course thats meant to be run from the shell, if you want to turn it to a script you will have to add the shebang. The value you want will be in $total. So what do you mean you cannot figure out how to execute this ? – topdog Jul 27 '10 at 19:09
  • Which shell? If I paste this into a bash command line (on Ubuntu), I get: "The program 'total' is currently not installed. You can install it by typing: sudo apt-get install radiance" – Daryl Spitzer Jul 27 '10 at 19:34
  • 1
    The spaces need to be removed from the "total = 0" line. And you'll need to echo $total to see what the actual total is. – Alex Jul 27 '10 at 19:52

You can take your original solution and pipe into a summing program in awk:

find . -name "test.save*" -exec wc -l {} \; | \
  awk '{count += $1} END {print count}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.