1

I want to get the total count of the number of lines from all the files returned by the following command:

shell> find . -name *.info

All the .info files are nested in sub-directories so I can't simply do:

shell> wc -l *.info

Am sure this should be in any bash users repertoire, but am stuck!

Thanks

Richard H
  • 38,037
  • 37
  • 111
  • 138

6 Answers6

2

You can use xargs like so:

find . -name *.info -print0 | xargs -0 cat | wc -l
a'r
  • 35,921
  • 7
  • 66
  • 67
  • 1
    Don't forget to quote your shell meta-characters or else you might wonder why you're getting odd results or odd errors from find: find . -name '*.info' -print0 | xargs -0 cat | wc -l – Adrian Pronk Aug 04 '10 at 11:36
2
wc -l `find . -name *.info`

If you just want the total, use

wc -l `find . -name *.info` | tail -1

Edit: Piping to xargs also works, and hopefully can avoid the 'command line too long'.

find . -name *.info | xargs wc -l
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

some googling turns up

find /topleveldirectory/ -type f -exec wc -l {} \; | awk '{total += $1} END{print total}'

which seems to do the trick

second
  • 28,029
  • 7
  • 75
  • 76
0
#!/bin/bash
# bash 4.0
shopt -s globstar
sum=0
for file in **/*.info
do
   if [ -f "$file" ];then
       s=$(wc -l< "$file")
       sum=$((sum+s))
   fi
done
echo "Total: $sum"
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0
find . -name "*.info" -exec wc -l {} \;

Note to self - read the question

find . -name "*.info" -exec cat {} \; | wc -l
JeremyP
  • 84,577
  • 15
  • 123
  • 161
0
# for a speed-up use: find ... -exec ... '{}' + | ...
find . -type f -name "*.info" -exec sed -n '$=' '{}' + | awk '{total += $0} END{print total}'
zaga
  • 1