Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I do a line count on all the PHP scripts within my webroot?

I am trying something like this below to no avail:

wc -l *.php
share|improve this question
Do you search the line count in summary or for each php file separately? – user unknown Apr 4 '12 at 14:45
How is wc -l *.php not working? Do you need it recursive? – Kevin Apr 4 '12 at 15:54
@Kevin Yes, it needed to be a recursive count. – crmpicco Apr 4 '12 at 16:35
up vote 1 down vote accepted

You need to use either a shell whose wildcard expansion includes subdirectories, or to stack another tool for directory transversal, such as find:

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

If, OTOH, your goal is to sum it all, join the code first:

find -name "*.php" | xargs cat | wc -l

share|improve this answer
Hmm...both options give an error of "find: paths must precede expression" – crmpicco Apr 4 '12 at 10:24
my bad, fixed to avoid wildcard expansion when there are *.php files in the current directory. – njsg Apr 4 '12 at 10:25
Thanks - the second option is exactly what I was looking for. – crmpicco Apr 4 '12 at 10:28
Useless use of xargs. find -name "*.php" -exec cat {} + | wc -l – user unknown Apr 4 '12 at 14:43
Not white space safe, use -print0/-0 or -exec – Kevin Apr 4 '12 at 16:49

With bash (only since version 4.0) you can do this:

shopt -s globstar
wc -l **/*.php

In zsh, you can always do wc -l **/*.php.

share|improve this answer
I'm using Bash 3.2.25 and get the following error: "-bash: shopt: globstar: invalid shell option name" – crmpicco Apr 4 '12 at 10:26

You need to enable bashs globstar option, so ** matches all files and subdirectories:

shopt -s globstar
wc -l **/*.php

There are also tools like cloc made for generating such statistics.

share|improve this answer
Note: only bash 4+ – Kevin Apr 4 '12 at 16:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.