How to count all the lines of code in a directory recursively?
We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories. We don't need to ignore comments, as we're just trying to get a rough idea.
wc -l *.php
That command works great within a given directory, but ignores subdirectories. I was thinking this might work, but it is returning 74, which is definitely not the case...
find . -name '*.php' | wc -l
What's the correct syntax to feed in all the files?
Try:
find . -name '*.php' | xargs wc -l
The SLOCCount tool may help as well.
It'll give an accurate source lines of code count for whatever hierarchy you point it at, as well as some additional stats.
For another one-liner:
( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l
works on names with spaces, 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.).
下一篇: 如何递归计算目录中的所有代码行?