[SHELL] Count lines of code in all files recursively

Here a unix command line script to count code lines for your project (or something else!).

In this example it counts lines in an Objective-C iOS Application (.h or .m files).

[code lang=”bash” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]$
$find . \( -name ‘*.h’ -o -name ‘*.m’ \) -print -exec cat {} \; | wc -l
25812
$[/code]


Explanation:

[code lang=”bash” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]find . \( -name ‘*.h’ -o -name ‘*.m’ \) -print[/code]

– list all files endings with .H or .M recursively from current folder (.).

[code lang=”bash” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]-exec cat {} \;[/code]

– while it finds files, exec a cat on current file

[code lang=”bash” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]wc -l[/code]

– count lines of current file and sum it to total.

25812 lines of code! Sounds good!

enjoy!

Alberto Pasca