in reply to Re: Count file lines in a directory tree
in thread Count file lines in a directory tree

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

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

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

Replies are listed 'Best First'.
Re^3: Count file lines in a directory tree
by Aristotle (Chancellor) on Nov 09, 2005 at 02:25 UTC

    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.