Monday, August 29, 2011

Count source code lines roughly by command line

I started to understand the command line a little bit more.

Today, I met a problem, I need to count the lines of my project. My first reaction is to use the `find` and `wc` command. OK, first I got this:

find . -type f \( -name "*.h" -or -name "*.m" \) -exec wc -l {} \;

OK, it works, but, all the line numbers are for each file, there is no summary information such as total number of lines which is the most important data I wanted. After googling, I got a hit on Stackoverflow.com, here is the correct solution. Also using the same command, but with a different order. This time, this command generated what I wanted.



[Update 1@Aug 30, 2011]
The above method is still not good enough. A good example is that when the file name or folder name contains space, the above command could not work properly with this kind of file name/folder name. How to resolve it?

Here is another way:



To understand this, you need to know some basic ideas behind Unix command line. Please see detail of awk@wikipedia, pipeline@wikipedia. About using `-or` in find command, please refer to the man page.

Command line is powerful, use it often and wisely. It will save you a lot of time.

No comments:

Post a Comment