How can I count all the lines of code in a directory recursively? How can I count all the lines of code in a directory recursively? shell shell

How can I count all the lines of code in a directory recursively?


Try:

find . -name '*.php' | xargs wc -l

or (when file names include special characters such as spaces)

find . -name '*.php' | sed 's/.*/"&"/' | xargs  wc -l

The SLOCCount tool may help as well.

It will give an accurate source lines of code count for whateverhierarchy you point it at, as well as some additional stats.

Sorted output:

find . -name '*.php' | xargs wc -l | sort -nr


For another one-liner:

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

It works on names with spaces and only outputs one number.


If using a decently recent version of Bash (or ZSH), it's much simpler:

wc -l **/*.php

In the Bash shell this requires the globstar option to be set, otherwise the ** glob-operator is not recursive. To enable this setting, issue

shopt -s globstar

To make this permanent, add it to one of the initialization files (~/.bashrc, ~/.bash_profile etc.).