in reply to Count file lines in a directory tree

You mean this?

find -name '*.p[lm]' -print0 | xargs -r0 cat | wc -l

:-)

Update: I was using the unnecessarily longwinded find \( -name '*.pm' -o -name '*.pl' \)

Update: to make this output the number of files, the easiest approach is a bit of Perl:

find -name '*.p[lm]' -print0 \ | perl -00000pe'++$a;END{print STDERR "Files: ",$a||0,", lines: "}' \ | xargs -r0 cat | wc -l

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Count file lines in a directory tree
by sauoq (Abbot) on Nov 09, 2005 at 02:12 UTC

    That's a useless use of... xargs.

    find -name '*.p[lm]' -exec cat {} \; | wc -l
    :-)

    -sauoq
    "My two cents aren't worth a dime.";
    

      Congratulations, if you have 10,000 matching files, that line makes you a cat herder. ;-) And a herder of useless cats, to boot…

      If you’re going to do that, you can just do

      find -name '*.p[lm]' -exec wc -l {} \; | awk '{ sum+=$1 } END { print +sum }'

      Moves a bit less data around. It also makes it trivial to count the number of files, as GrandFather’s code does:

      find -name '*.p[lm]' -exec wc -l {} \; \ | awk '{ sum+=$1; ++num } END { print sum "lines in" num "files" }'

      Nice catch on the glob. :-)

      The xargs in mine is quite justified, so I don’t run one process per matched file. The cat is the easiest solution to the problem that if too many files for a single commandline are matched, xargs wc would run the wc multiple times, reporting multiple disjunct totals. This way, xargs runs multiple cats (but very few in total), but wc always run exactly once.

      Makeshifts last the longest.