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/QuestionAdapter.java
44   ./com/epc/uscis/adapters/QuestionHolder.java
412  ./com/epc/uscis/db/QuestionHelper.java
175  ./com/epc/uscis/ui/AnswerActivity.java
348  ./com/epc/uscis/ui/BrowseQuestionsActivity.java
101  ./com/epc/uscis/ui/HomeActivity.java
451  ./com/epc/uscis/ui/QuestionActivity.java
45   ./com/epc/uscis/util/Util.java
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/NoFlaggedQuestionsActivity.java
  40 ./com/epc/uscis/adapters/QuestionAdapter.java
  42 ./com/epc/uscis/adapters/QuestionHolder.java
  45 ./com/epc/uscis/util/Util.java
  85 ./com/epc/uscis/ui/HelpActivity.java
 118 ./com/epc/uscis/ui/HomeActivity.java
 172 ./com/epc/uscis/ui/AnswerActivity.java
 424 ./com/epc/uscis/db/QuestionHelper.java
 484 ./com/epc/uscis/ui/QuestionActivity.java
 591 ./com/epc/uscis/ui/BrowseQuestionsActivity.java
2033 total
[ed@ src]$

I just love easy, simple solutions!

Leave a comment