Easily sum matching file line counts

This command will return the number of total lines contained in files ending in .py in the directory it is run in, as well as subdirectories:

1
2
find . -type f -iname *py | xargs -I str wc -l str | \
awk '{SUM += $1} END {print SUM}'

Just change .py to whatever file type you want to get the count.

I wanted to add this as a Bash alias, passing in the filetype as an argument. This would make the whole affair much easier. But when I tried it as an alias, or as a function, I always got 0 back. Running “type” on the alias name returned:

1
2
pylines is aliased to `find . -type f -iname *py | xargs -I str \
wc -l str | awk '{SUM += bell-style} END {print SUM}''

If anyone has any insight into this, please share.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. Get useful image information on the command line
  2. Useful Bash functions to determine OS and more
  3. Viewing all users on a Linux system

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in CLI and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>