While working on different projects, sometimes you might be required to provide a report or statistics of your progress, or simply to calculate the value of your code.
There is this simple yet powerful tool called “cloc – count lines of code” that allows you to count all number of your code and exclude comments and blank lines at the same time.
It is available in all major Linux distributions and supports multiple programming languages and file extensions and does not have any specific requirements to be used.
How to Install and Use Cloc in Linux Systems
Installing cloc is easy and simple. Below you can see how to install cloc in different operating systems with their related package managers:
$ sudo apt install cloc # Debian, Ubuntu $ sudo yum install cloc # Red Hat, Fedora $ sudo dnf install cloc # Fedora 22 or later $ sudo pacman -S cloc # Arch $ sudo emerge -av dev-util/cloc # Gentoo https://packages.gentoo.org/packages/dev-util/cloc $ sudo apk add cloc # Alpine Linux $ sudo pkg install cloc # FreeBSD $ sudo port install cloc # Mac OS X with MacPorts $ brew install cloc # Mac OS X with Homebrew $ npm install -g cloc # https://www.npmjs.com/package/cloc
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.
Here is an example from a file in bash. The file in question contains the following code in bash:
$ cat bash_script.sh
Now lets run cloc on it.
$ cloc bash_script.sh
Another cool feature of cloc is that can even be used on compressed files. For example, I have downloaded the latest WordPress archive and ran cloc on it.
$ cloc latest.tar.gz