Counting lines of code?

if I want to count the lines of code, the trivial thing is

cat *.c *.h | wc -l

But what if I have several subdirectories?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The easiest way is to use the tool called cloc. Use it this way:

cloc .

That’s it. 🙂

Method 2

You should probably use SLOCCount or cloc for this, they’re designed specifically for counting lines of source code in a project, regardless of directory structure etc.; either

sloccount .

cloc .

will produce a report on all the source code starting from the current directory.

If you want to use find and wc, GNU wc has a nice --files0-from option:

find . -name '*.[ch]' -print0 | wc --files0-from=- -l

(Thanks to SnakeDoc for the cloc suggestion!)

Method 3

As the wc command can take multiple arguments, you can just pass all the filenames to wc using the + argument of the -exec action of GNU find:

find . -type f -name '*.[ch]' -exec wc -l {} +

Alternately, in bash, using the shell option globstar to traverse the directories recursively:
shopt -s globstar
wc -l **/*.[ch]

Other shells traverse recursively by default (e.g. zsh) or have similar option like globstar, well, at least most ones.

Method 4

If you are in an environment where you don’t have access to cloc etc I’d suggest

find -name '*.[ch]' -type f -exec cat '{}' + | grep -c '[^[:space:]]'

Run-through: find searches recursively for all the regular files whose name ends in either .c or .h and runs cat on them. The output is piped through grep to count all the non-blank lines (the ones that contain at least one non-spacing character).

Method 5

You can use find together with xargs and wc:

find . -type f -name '*.h' -o -name '*.c' | xargs wc -l

Method 6

As has been pointed out in the comments, cat file | wc -l is not equivalent to wc -l file because the former prints only a number whereas the latter prints a number and the filename. Likewise cat * | wc -l will print just a number, whereas wc -l * will print a line of information for each file.

In the spirit of simplicity, let’s revisit the question actually asked:

if I want to count the lines of code, the trivial thing is

cat *.c *.h | wc -l

But what if I have several subdirectories?

Firstly, you can simplify even your trivial command to:

cat *.[ch] | wc -l

And finally, the many-subdirectory equivalent is:
find . -name '*.[ch]' -exec cat {} + | wc -l

This could perhaps be improved in many ways, such as restricting the matched files to regular files only (not directories) by adding -type f—but the given find command is the exact recursive equivalent of cat *.[ch].

Method 7

Sample using awk:

find . -name '*.[ch]' -exec wc -l {} ; |
  awk '{SUM+=$1}; END { print "Total number of lines: " SUM }'

Method 8

easy command:

find . -name '*.[ch]' | xargs wc -l

Method 9

If you’re on Linux I recommend my own tool, polyglot. It’s dramatically faster than cloc and more featureful than sloccount.

You should be able to build on BSD as well, though there aren’t any provided binaries.

You can invoke it with

poly .

Method 10

The new bid on the cloc is Loci.
Link to NPM package
It counts code similarly to cloc, but is faster at scale.
Also, as its natively written in nodejs it will run on all other environments without Perl (for or cloc.exe.

It is in its infancy, but you can install it as an NPM CLI tool, or import it as a library into your own project.

Great for environments where you can install script-based npms, but are not allowed to use unapproved binaries

Method 11

find . -name *.[ch] -print | xargs -n 1 wc -l should do the trick. There are several possible variations on that as well, such as using -exec instead of piping the output to wc.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments