1

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
  • 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
1

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

  • 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
  • 1
    Useless use of xargs. find -name "*.php" -exec cat {} + | wc -l – user unknown Apr 4 '12 at 14:43
  • 1
    Not white space safe, use -print0/-0 or -exec – Kevin Apr 4 '12 at 16:49
2

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.

  • 1
    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
1

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.

  • 1
    Note: only bash 4+ – Kevin Apr 4 '12 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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