Tuesday, January 13, 2009

Counting code lines.

I was curious how many lines of code my Java application had. I found this script very useful in finding out.

Here it is, in case it is helpful:

find . -name "*java" -exec wc -l {} \; | grep -v "/test/" | awk '{print sum+=$1} END {print "Sum: "sum}'

First it finds all files with the extension java (change to cpp, or pl, or py if you need to). It runs wc -l on each file to count the lines.

Then it removes my test directory, you can use -vE for a regular expression of items to remove or remove that command all together.

I then use awk to sum up the file line count and print out the increasing amount as well as the sum at the end.

Hope that helps.

No comments: