13

Using the command:

wc -l + `find . -name \* -print` 

You can get the total number of lines of all files inside a folder.

But imagine you have some folders (for example libraries), which you don't want to count their lines because you didn't write them.

So, how would you count the lines in a project excluding certain folders?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
rfc1484
  • 9,441
  • 16
  • 72
  • 123

4 Answers4

27

cloc has always been a great friend whenever I need to count lines of src-code. Using 2.6.29 linux kernel as an example:

$ cloc .

   26667 text files.
      26357 unique files.
          2782 files ignored.

http://cloc.sourceforge.net v 1.50  T=168.0 s (140.9 files/s, 58995.0 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
C                             11435        1072207        1141803        5487594
C/C++ Header                  10033         232559         368953        1256555
Assembly                       1021          35605          41375         223098
make                           1087           4802           5388          16542
Perl                             25           1431           1648           7444
yacc                              5            447            318           2962
Bourne Shell                     50            464           1232           2922
C++                               1            205             58           1496
lex                               5            222            246           1399
HTML                              2             58              0            378
NAnt scripts                      1             85              0            299
Python                            3             62             77            277
Bourne Again Shell                4             55             22            265
Lisp                              1             63              0            218
ASP                               1             33              0            136
awk                               2             14              7             98
sed                               1              0              3             29
XSLT                              1              0              1              7
--------------------------------------------------------------------------------
SUM:                          23678        1348312        1561131        7001719
--------------------------------------------------------------------------------
vmassuchetto
  • 1,529
  • 1
  • 20
  • 44
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
5

With find, you can also "negate" matching conditions with !. For example, if I want to list all the .java files in a directory, excluding those containing Test:

find . -name "*.java" ! -name "*Test*"

Hope this helps!

Edit:

By the way, the -name predicate only filters file names. If you want to filter paths (so you can filter directories), use -path:

find . -path "*.java" ! -path "*Test*"

PaoloVictor
  • 1,296
  • 8
  • 18
0

you could always exclude them by listing out the files using regular expressions, for example,

*.txt will include only txt files and so on...

ConfusedAboutCPP
  • 573
  • 3
  • 8
  • 20
0

I made an NPM package specifically for this usage, which allows you to call a CLI tool and providing the directory path and the folders/files to ignore

it goes like:

npm i -g @quasimodo147/countlines

to get the $ countlines command in your terminal

then you can do countlines . node_modules build dist

Mohamed Belkamel
  • 359
  • 1
  • 2
  • 8