0

I'm trying to figure out how many lines of code I have for files that end in *.pp

 find . -name *.pp -type f  | wc -l
  39

I know that I have 39 files, how can I figure out how many lines of code those 39 files contain?

4 Answers 4

4

Try this version:

$ find . -iname "*.pp" -type f  -exec wc -l {} +

Here's an example using .cpp files:

$ find . -iname "*.cpp" -type f -exec wc -l {} +
  229 ./jruby-launcher-1.0.7-java/utilsfuncswin.cpp
  269 ./jruby-launcher-1.0.7-java/utilsfuncs.cpp
   84 ./jruby-launcher-1.0.7-java/jrubyexe.cpp
   85 ./jruby-launcher-1.0.7-java/unixlauncher.cpp
  250 ./jruby-launcher-1.0.7-java/platformlauncher.cpp
  593 ./jruby-launcher-1.0.7-java/argparser.cpp
  452 ./jruby-launcher-1.0.7-java/jvmlauncher.cpp
   77 ./jruby-launcher-1.0.7-java/jruby.cpp
 2039 total

I believe if you have more files than will fit on a single command line call to wc that it will give you a total per each call, but this can be modified to do a total of totals if needed.

2

The following will do the job

find . -name *.pp -type f -exec wc -l {} \;
0

What's wrong with using the wc command on its own?!
wc -l *.pp would yield the expected result (if all the files are in the same folder, although you could expand on it by doing wc -l FolderA/*.pp FolderB/*.pp)
And if you are only after the grandtotal, without individual file results:
find . -name "*.pp" -type f -exec cat {} + | wc -l

0

@spuder attempted to use wc in this command to count "*.pp" files:

find . -name *.pp -type f  | wc -l

There are a few problems:

  • the glob expression should be quoted so that is passed to wc
  • wc does not open the filenames passed as input and count those
  • wc is counting total lines in the file.

OP did not explain what the files were. There are a few possibilities, most more obscure than what appears in OP's profile. Given that profile, the ".pp" suffix refers to Puppet files, which are essentially a metadata declaration in Ruby syntax. Those files can have comments (beginning with #) and blank lines.

The usual interpretation of lines of code ignores those. Whether you want physical lines of code (total lines ignoring comments and blank lines) or source lines of code (the number of statements) depends on how picky you want to be.

Puppet's syntax is more complicated than that (it also allows C-style comments and has quoted strings), but for the sake of example, here is a very simple filter that trims out # comments and blank lines (using the examples directory of Puppet 2.7.10)

find . -type f -name "*.pp" -exec sed -e '/^[[:space:]]*#/d' -e '/^[[:space:]]*$/d' {} + |wc -l
 429

in contrast to unfiltered:

find . -type f -name "*.pp" -exec cat {} + |wc -l
477 

Further reading:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .