Count lines of code across multiple file types, sorted by least amount of code to greatest

find . \( -iname '*.[ch]' -o -iname '*.php' -o -iname '*.pl' \) | xargs wc -l | sort -n
find -exec is evil since it launches a process for each file. You get the total as a bonus. Also, without -n sort will sort by lexical order (that is 9 after 10).

By: rbossy
2010-04-30 12:21:28

2 Alternatives + Submit Alt

What Others Think

I like the -n addition to sort but you should use -print0/xargs -0 to avoid problems directories or files that contain spaces
rkulla · 641 weeks and 1 day ago
find . \( -iname '*.[ch]' -o -iname '*.php' -o -iname '*.pl' \) -print0 | xargs -0 wc -l | sort -n
rkulla · 641 weeks and 1 day ago
Exec is always evil. If you're using GNU find, you can use -exec without all the forks by simply using + (plus) instead of ; (semicolon) to mark the end of the command line. Additional bonus: the plus character isn't special to the shell so it doesn't have to be quoted.
hackerb9 · 640 weeks and 6 days ago
DId I just say "always"? I meant "not always".
hackerb9 · 640 weeks and 6 days ago
@rkulla: well spotted @hackerb9: thanks for teaching me something about find. Cool...
rbossy · 640 weeks and 5 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: