Count lines of code in files recursively

January 26, 2013

If you are attempting to find a count of the total lines of code in files in a directory recursively from Linux or Mac OS X the following command will do what you want. This particular command prints out all lines contained in any file with a .php extension, then counts the number of lines printed.

( find . -name '*.php' -print0 | xargs -0 cat ) | wc -l

2 thoughts on “Count lines of code in files recursively

  1. Ricardo (July 7, 2019)

    This version allows the usage of regular expressions, which may be more flexible than the “find” command global expressions.

    (find . | grep -E “\\.cc|\\.c|\\.h” | tr ‘\n’ ‘\0’ | xargs -0 cat) | wc -l

  2. Tim Sampson (November 16, 2019)

    There’s also some apps in the App Store for this such as SourceCounter and Xloc.

Leave a Reply

Your email address will not be published. Required fields are marked *