Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

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?

share|improve this question

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.

share|improve this answer

The following will do the job

find . -name *.pp -type f -exec wc -l {} \;
share|improve this answer

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

share|improve this answer

@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:

share|improve this answer

Your Answer

 
discard

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.