How to count lines of code
Tags: webdev, beginners, bash, javascript
Published: 2024-03-12 11:59:32

This post if a followup for the previous one, on how to [count lines in text](https://dev.to/websilvercraft/how-to-count-lines-in-javascript-59k2) or in a in a text file in Javascript. In this post I'm going to show how to count the lines of code in a project directory or any directory containing source code files of any type.

## Using find, wc and xargs

In unix-linux based systems we can use find wc and xargs to count lines in files based on their extension. 

```bash
find . -name '*.py' | xargs wc -l
```

Excluding directories:

```bash
find . -name '*.py' -not -path './exclude-dir/*' | xargs wc -l
```

Handling Spaces in Filenames

If the files or directories have spaces in their names, it’s safer to use find ... -print0 with xargs -0 to handle these names correctly.

```bash
find . -name '*.py' -print0 | xargs -0 wc -l
```

* `find . -name '*.py' -print0`: This finds all files ending with .py starting from the current directory (.). It prints the list of found files with each filename followed by a null character (\0) instead of a newline. This null character handling allows for filenames with special characters (including spaces) to be processed correctly by xargs.
* `xargs -0 wc -l`: The -0 option tells xargs to expect null-terminated strings as input (matching the output of find ... -print0). xargs passes the filenames to wc -l, which counts the lines in each file. The output is a list of line counts followed by the corresponding filename.

Counting Lines for Multiple File Types

```bash
find . \( -name '*.py' -o -name '*.js' \) -print0 | xargs -0 wc -l
```

Summing Total Lines

```bash
find . -name '*.py' | xargs wc -l | tail -n 1
```

Using awk to "accumulate" line count

```bash
find . -name '*.py' -print0 | xargs -0 wc -l | awk '{total += $1} END {print total}'
```

`awk '{total += $1} END {print total}'`: This awk script sums up the line counts. It reads the line counts (which are the first whitespace-delimited field $1 in the wc -l output) and accumulates them in the total variable. After processing all input lines, the END block prints the total sum of lines.

## Using cloc(Count Lines of Code) 

`cloc` (Count Lines of Code) is a specialized command line tool that not only counts lines of text, but also distinguishes between code, comments, and blank lines across multiple programming languages. 

```bash
cloc .
```

```bash
github.com/AlDanial/cloc v 1.82  T=2.12 s (1500.2 files/s, 240327.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                    2257          39250          72045         199230
JSON                           229              9              0          39756
Markdown                       308          13894              2          39106
CSS                             21           2721            151          36892
Python                          49           4343           7763          21745
C/C++ Header                    10           1465            916           8019
Sass                            88           1339           1297           6797
TypeScript                     110            451           1794           2616
C++                              6            392             99           1840
Handlebars                      36            424             81           1184
HTML                             5              5              0            692
YAML                            33             37             34            567
SVG                              1              0            101            500
Lisp                             2             42             38            258
C#                               1             55              9            186
XML                              5              0              0             72
SQL                              2             11             16             66
make                             5             25              4             60
Bourne Shell                     3              4              6             26
Nix                              1              1              0             19
DOS Batch                        2              1              0              6
-------------------------------------------------------------------------------
SUM:                          3174          64469          84356         359637
-------------------------------------------------------------------------------
```

Advanced Options

Filter by Specific Languages:


```
cloc --include-lang=Python,JavaScript .
    3843 text files.
    3510 unique files.                                          
    1553 files ignored.

github.com/AlDanial/cloc v 1.82  T=1.33 s (1734.4 files/s, 259007.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                    2257          39250          72045         199230
Python                          49           4343           7763          21745
-------------------------------------------------------------------------------
SUM:                          2306          43593          79808         220975
-------------------------------------------------------------------------------
```

Exclude Directories:

```
cloc --exclude-dir=node_modules .
     131 text files.
     117 unique files.                                          
      49 files ignored.

github.com/AlDanial/cloc v 1.82  T=0.14 s (673.5 files/s, 480769.4 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
CSS                             18           2707            145          36722
JavaScript                      29           4703           1679          13994
JSON                             4              0              0           3862
Handlebars                      36            424             81           1184
SQL                              2             11             16             66
Markdown                         3             20              0             63
-------------------------------------------------------------------------------
SUM:                            92           7865           1921          55891
-------------------------------------------------------------------------------
```

If you liked this post, give a thumb up and check my other post on [how to count the number lines](https://dev.to/websilvercraft/how-to-count-lines-in-javascript-59k2) in a text or text files in javascript, or use my online tool to [count lines](https://blankhtml.com/text-tools/count-lines/) in a text.