Make a statistic about the lines of code

find . -type f -name '*.c' -exec wc -l {} \; | awk '{sum+=$1} END {print sum}'
Have wc work on each file then add up the total with awk; get a 43% speed increase on RHEL over using "-exec cat|wc -l" and a 67% increase on my Ubuntu laptop (this is with 10MB of data in 767 files).

By: arcege
2009-09-04 15:51:30

2 Alternatives + Submit Alt

What Others Think

wc can take multiple files as arguments, so you REALLY want to replace \; with +, enabling the "xargs mode" of the -exec option: find . -type f -name "*.c" -exec wc -l {} + | awk '{t+=$1} END {print t}'
bwoodacre · 663 weeks and 3 days ago
The "+" option is not a standard feature of find. Not all versions of Linux will support it, let alone the different flavors of UNIX out there.
arcege · 663 weeks and 3 days ago
That's too bad since I find + so useful and have recommended it to so many on commandlinefu! Although for really kinky things you want xargs which more systems have, so I can see why "feature duplication" is avoided by some. I tested it on Redhat, Ubuntu, and Debian and it works there. What non-dead UNIXes do not support the feature? I haven't been very successful at googling.
bwoodacre · 662 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: