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.

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

No related posts.

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>