Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count total number of lines in a project excluding certain folders or files

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?

like image 202
rfc1484 Avatar asked May 16 '11 11:05

rfc1484


People also ask

How do you count lines of code in a project?

Cloc can be used to count lines in particular file or in multiple files within directory. To use cloc simply type cloc followed by the file or directory which you wish to examine. Now lets run cloc on it. As you can see it counted the number of files, blank lines, comments and lines of code.

How do you count lines of code in a Python project?

Use os. walk to traverse the files and sub directories, use endswith to filter the files you want to count, open each file and use sum(1 for line in f) to count the lines, aggregate all the file line counts.

How do you count all lines in all files in a directory?

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count. However, in this case, we are using this command to count the number of files in a directory.

How can I count all the lines of code in a directory recursively Windows?

To do that, you would use dir -Recurse -Include *. txt .


2 Answers

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*"

like image 157
PaoloVictor Avatar answered Nov 16 '22 01:11

PaoloVictor


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
--------------------------------------------------------------------------------
like image 38
Fredrik Pihl Avatar answered Nov 16 '22 02:11

Fredrik Pihl