Friday, April 16, 2010

Recursive Line Count (Not Code Count)

A common question I am asked is how to count the total number of lines in specific files of a specific directory (and its subdirectories). After navigating to the root of the directory in which you are interested, type:

find . -type f | xargs wc -l

This will find all files, recursively, and pass them (as arguments) into the standard word count utility; however, if you would like to specify specific types of files, try using a regular expression with the find utility:

find . \( ! -regex '.*/\..*' \) -type f | xargs wc -l

This line of code will only pass non-hidden files (those that do not start with a period) into wc. This would be handy if, say, you were using SVN and did not want to parse .svn directories. Simple edits to this regular expression will parse only .html or files that begin with temp.

My guess is that most people who ask this question are interested in counting total lines of code in some project. Typically, this is not a smart way to count code, as blank lines, comments, split lines, and brackets – not just real lines of code – will be counted. If you are looking for a more intelligent code counter, check out CLOC. I apologize for recommending a Perl program, but it works.


Post a Comment

Subscribe to Post Comments [Atom]

<< Home