Alex Rivera | Logout

Utility To Count Number Of Lines Of Code In Python Or Bash

Asked 2011-09-21T23:16:02.990
20

Is there a quick and dirty way in either python or bash script, that can recursively descend a directory and count the total number of lines of code? We would like to be able to exclude certain directories though.

For example:

start at: /apps/projects/reallycoolapp
exclude: lib/, frameworks/

The excluded directories should be recursive as well. For example:

/app/projects/reallycool/lib SHOULD BE EXCLUDED
/app/projects/reallycool/modules/apple/frameworks SHOULD ALSO BE EXCLUDED

This would be a really useful utility.

Edit
Report

1 Answer

14

The find and wc arguments alone can solve your problem.

With find you can specify very complex logic like this:

find /apps/projects/reallycoolapp -type f -iname '*.py' ! -path '*/lib/*' ! -path '*/frameworks/*' | xargs wc -l

Here the ! invert the condition so this command will count the lines for each python files not in 'lib/' or in 'frameworks/' directories.

Just dont forget the '*' or it will not match anything.

answered 2011-09-22T05:25:35.550

Your Answer