Bash: how to transfer each result of one command to another

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

shell> find . -name *.info 

All .info files are nested in subdirectories, so I can't just do:

 shell> wc -l *.info 

I am sure that this should be in any repertoire of bash users, but it’s stuck!

thanks

+1
source share
6 answers
 wc -l `find . -name *.info` 

If you just want to get the total, use

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

Edit: also works with the xargs channel, and hopefully can avoid a command line that is too long.

 find . -name *.info | xargs wc -l 
+2
source

You can use xargs like this:

 find . -name *.info -print0 | xargs -0 cat | wc -l 
+2
source

some googling appears

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

which seems to be doing the trick

+1
source
 #!/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" 
0
source
 find . -name "*.info" -exec wc -l {} \; 

Note for self-read question

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

All Articles