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
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.
xargs
find . -name *.info | xargs wc -l
You can use xargs like this:
find . -name *.info -print0 | xargs -0 cat | wc -l
some googling appears
find /topleveldirectory/ -type f -exec wc -l {} \; | awk '{total += $1} END{print total}'
which seems to be doing the trick
#!/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"
find . -name "*.info" -exec wc -l {} \;
Note for self-read question
find . -name "*.info" -exec cat {} \; | wc -l
# for a speed-up use: find ... -exec ... '{}' + | ... find . -type f -name "*.info" -exec sed -n '$=' '{}' + | awk '{total += $0} END{print total}'