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

user77413 picture user77413 · Aug 31, 2009 · Viewed 833.5k times · Source

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 it 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?

Answer

Peter Elespuru picture Peter Elespuru · Aug 31, 2009

Try:

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

The SLOCCount tool may help as well.

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

Sorted output:

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