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. The Linux distro might have an old / slow CLOC version. It can be worth downloading the current version of CLOC if you use it a lot.

CLOC works on any operating system since it’s just Perl script.

For Linux / macOS: download latest cloc-*.pl to ~/.local/bin. Be sure $HOME/.local/bin is on your PATH by adding to ~/.profile:

export PATH=$HOME/.local/bin:$PATH

For convenience, make a softlink for cloc:

ln -s ~/.local/bin/cloc-*.pl ~/.local/bin/cloc

Make sure it’s executable:

chmod +x ~/.local/bin/cloc

For Windows download latest cloc-*.exe to c:\cloc\ and add c:\cloc to the user PATH.


To make CLOC 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 .

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

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

cloc --categorized=cloc.log