How to count lines of code in PHP files in a web application

| | 1 min read

Lines of code is typically indicative of effort that goes towards software development. So how do you count lines of code in your PHP application (or for that matter any specific types of files in any application). You can't manually open and count files, that would be a big waste of time. The *nix shell has a powerful set of tools that can help you count lines without having to do it manually.


find . -name "*.php" | xargs wc -l | awk '{print $1}' | awk '{total = total + $1}END{print total}'

The find command finds files named *.php (you can replace this with other patterns or sets of patterns with -o and -a). These files are passed to wc using xargs. wc prints out the list of files with the number of lines in each. The tabular output is then processed using awk to separate out just the column of line counts. This is then again processed using a bit of awk scripting.

I hope you got the general idea here on how these commands work to give you the number of lines of code in your PHP files. Happy coding.