tags:

views:

83

answers:

6

Hi All,

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

+2  A: 

You can use xargs like so:

find . -name *.info -print0 | xargs -0 cat | wc -l
ar
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
+1  A: 

some googling turns up

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

which seems to do the trick

second
+2  A: 
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
ah of course back ticks. marking you correct for being the most simple/elegant solution
Richard
Hope you don't have too many files. Beware the dreaded "command line too long"
Adrian Pronk
@Adrian: yes - I had not considered that, so maybe not the "right" answer after all. (Although works for me: 2,500 files)
Richard
A: 
#!/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
A: 
find . -name "*.info" -exec wc -l {} \;

Note to self - read the question

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