Count lines of project code with CLOC

CLOC is a single-file Perl program that counts lines of code very quickly in large projects. If even more speed is needed, CLOC can run with parallel processes. Recent versions of CLOC are 10-100x faster than old versions. Ubuntu 18.04 has slow CLOC 1.74, so it’s worth to download the current version of CLOC.

CLOC works on any operating system since it’s just Perl, but this install example assumes Linux/MacOS/WSL

Linux/MacOS install

  1. download latest cloc-*.pl to ~/.local/bin. Be sure $HOME/.local/bin is on your PATH in general by adding to ~/.bashrc the line:
   export PATH=$HOME/.local/bin:$PATH
   
  1. for convenience, make a softlink for cloc:
   ln -s ~/.local/bin/cloc-*.pl ~/.local/bin/cloc
   
  1. make sure it’s executable:
   chmod +x ~/.local/bin/cloc
   

Parallel (optional)

If you want CLOC to run N times faster, you need the Perl Parallel::ForkManager library, which on Linux is installed by:

apt install libparallel-forkmanager-perl

then use like:

cloc --processes=4 .

Usage

For the GEMINI projects, we get a result like:

cloc --exclude-dir=vendor,archive,objects,subprojects  .

--exclude-dir
comma-separated list of directory names to exclude
     175 text files.
     168 unique files.
      22 files ignored.
github.com/AlDanial/cloc v 1.80  T=0.38 s (411.2 files/s, 69628.8 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Fortran 90                      35           3355           2809           9008
MATLAB                          71           1258           1202           4560
TeX                              1            266             76           1140
CMake                           20            315            119            857
Markdown                         3            175              0            366
make                             2             78             17            169
Bourne Shell                    13            113             97            160
INI                              6              1              0            126
Python                           4             65             15            122
NAnt script                      1             18              0             62
YAML                             1              7              9             19
-------------------------------------------------------------------------------
SUM:                           157           5651           4344          16589
-------------------------------------------------------------------------------

per-file listing

For debugging or finding what specific files are contributing to the total, use option:

cloc --categorized=cloc.log