As you work on your daily projects as an IT expert, you might find yourself in a situation where you are required to have a sampled report or statistics of your progress or you might just want to asses the value of your code. There are usually thousands of lines of code in ones project or just a few but even so, you cannot achieve accurate statistics without the help of a tool.
Among the best tools for the job is “cloc – count lines of code” ,which is a very powerful tool which allows you to count the number of lines of code and also the blanks and comments.
“cloc” is available in all major linux distributions and supports a variety of programming languages and file extensions. It also has a couple of incredible features that put it at the top compared to others.
The following are some of its features;
- Exists as a single, self-contained file that takes very little installation effort—just download and run the file.
- Can read language comment definitions from a file, perhaps allowing it to function with computer languages that don’t exist yet.
- Allows you to combine results from several runs by language and project.
- Can produce results in a variety of formats: plain text, SQL, JSON, XML, YAML, comma separated value.
- Can count the number of lines of code in compressed archives (tar balls, Zip files, Java .ear files).
- It has several troubleshooting options.
Count Lines of source code in linux using cloc
In this tutorial,you are going to learn how to install and use cloc on your Linux systems. In all of the major Linux distros, cloc can be installed by just keying in one simple command and you are ready to go.
Below is a list of all the commands against their respective distros which can be used to install cloc.
# Debian, Ubuntu sudo apt install cloc # Red Hat, Fedora sudo yum install cloc # Fedora sudo dnf install cloc # Arch / Manjaro sudo pacman -S cloc # Gentoo https://packages.gentoo.org/packages/dev-util/cloc sudo emerge -av dev-util/cloc # Alpine Linux sudo apk add cloc # FreeBSD sudo pkg install cloc # macOS brew install cloc # Using npm https://www.npmjs.com/package/cloc npm install -g cloc
As you can see from the above list of commands, you have been given the commands you can use on various Linux distros to install cloc. As for me, am running Kali Linux, which is a debian-based Linux distribution and therefore, am required to use the following command;
sudo apt update sudo apt install cloc
Sample screenshot containing installation process.
Cloc usage example 1
In this example, I created a file called bash_script.sh using the cat command so that I could use cloc to count the lines of code in it including the blanks and comments. Take a look at the images below.
In the same image, you can see that to use cloc you simply type cloc, followed by the name of the file or directory you wish to evaluate. You can see the output which shows the number of evaluated files, blaks,comments and the number of lines of code.
Cloc usage example 2
In this second example, I used cloc on a downloaded package of google chrome in my desktop. See the demonstration below;
Cloc usage example 3
Among the cool features of cloc is that it can be used on compressed files. I have downloaded the latest tar installation file,which is a compressed archive and I ran cloc on it.
In case you need to get an analysis for multiple files in a directory you can use
“--by-file” option, that will count the lines in each file and provide an evaluation of each of the files.
Cloc also has some amazing extra options that can be used alongside the cloc command and add to its capability. I will provide a list of some of its options for which I am sure will be very helpful to some of you.
--by-fileReports results for every source file encountered.
--by-file-by-langReports results for every source file encountered in addition to reporting language.
--diff <set1> <set2>Computes the differences in code between the source files of set1 and set2. The input can be a mix of files, directories and archives.
--gitForces the inputs to be interpreted as git targets if they are not first identified as file or directory names.
--ignore-whitespaceIgnore horizontal white space when comparing files with
--ignore-caseIgnore changes is case within file contents; consider upper- and lowercase letters equivalent when comparing files with
--max-file-size=<MB>Skip files larger than the specified amount
--exclude-content=<regex>Exclude files containing texts that match the specified expression
--exclude-dir=<D1>[,D2,] Exclude the given comma separated directories D1,D2,D3 from being scanned.
--exclude-lang=<L1>[,L2[.....]]Exclude the given comma separated languages L1,L2,L3,L4 from being counted.
--include-lang=<L1>[,L2[.....]]Count only the given comma separated languages L1,L2,L3,L4 etc.
This article introduces you to cloc and also provides a guide on how the tool works. Cloc is a must have tool for any computer nerd even though you might not use it in your day to day activity. It really helps you when you are in need of its amazing capabilities.