Richard H
Richard H

Reputation: 39135

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

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

Upvotes: 1

Views: 1224

Answers (6)

zaga
zaga

Reputation: 1

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

Upvotes: 0

kennytm
kennytm

Reputation: 523684

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

Upvotes: 2

JeremyP
JeremyP

Reputation: 86691

find . -name "*.info" -exec wc -l {} \;

Note to self - read the question

find . -name "*.info" -exec cat {} \; | wc -l

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342967

#!/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"

Upvotes: 0

second
second

Reputation: 28655

some googling turns up

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

which seems to do the trick

Upvotes: 1

a&#39;r
a&#39;r

Reputation: 37029

You can use xargs like so:

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

Upvotes: 2

Related Questions