Counting Number of Lines of Code in a Project

1 minute read

One of the many reasons why I love working in a *nix environment is how easy it is to get what you want. For example, as I am reaching the end of my first android application I was in need to find out how many lines of code the whole project consisted of. I knew that I could use the wc utility along with the find function to find all files with the java extension. Anyhow, below is the single line of code that I executed on the command line to get the desired results:

[ed@ src]$ cd /home/edwin/uscis/src
[ed@ src]$ wc -l `find . -name *.java`

And got the following result:

40   ./com/epc/uscis/adapters/
44   ./com/epc/uscis/adapters/
412  ./com/epc/uscis/db/
175  ./com/epc/uscis/ui/
348  ./com/epc/uscis/ui/
101  ./com/epc/uscis/ui/
451  ./com/epc/uscis/ui/
45   ./com/epc/uscis/util/
1616 total

If you want to sort the results by the number of lines, you can type use the sort utility.

[ed@ src]$ wc -l `find . -name *.java` | sort
  32 ./com/epc/uscis/ui/
  40 ./com/epc/uscis/adapters/
  42 ./com/epc/uscis/adapters/
  45 ./com/epc/uscis/util/
  85 ./com/epc/uscis/ui/
 118 ./com/epc/uscis/ui/
 172 ./com/epc/uscis/ui/
 424 ./com/epc/uscis/db/
 484 ./com/epc/uscis/ui/
 591 ./com/epc/uscis/ui/
2033 total
[ed@ src]$

I just love easy, simple solutions!

Leave a comment