As a developer, you may need to share the progress and statistics of your code to your boss or colleagues. Your boss might want to analyze the code and give any additional inputs. In such cases, there are few programs, as far as I know, available to analyze the source code. One such program is Ohcount. Today, I came across yet another similar utility namely "Cloc". Using the Cloc, you can easily count the lines of source code in several programming languages. It counts the blank lines, comment lines, and physical lines of source code and displays the result in a neat tabular-column format. Cloc is free, open source and cross-platform utility entirely written in Perl programming language.
Table of Contents
Features
Cloc ships with numerous advantages including the following:
- Easy to install/use. Requires no dependencies.
- Portable
- It can produce results in a variety of formats, such as plain text, SQL, JSON, XML, YAML, comma separated values.
- Can count your git commits.
- Count the code in directories and sub-directories.
- Count codes count code within compressed archives like tar balls, Zip files, Java .ear files etc.
- Open source and cross platform.
Installing Cloc
The Cloc utility is available in the default repositories of most Unix-like operating systems. So, you can install it using the default package manager as shown below.
On Arch Linux and its variants:
$ sudo pacman -S cloc
On Debian, Ubuntu:
$ sudo apt-get install cloc
On CentOS, Red Hat, Scientific Linux:
$ sudo yum install cloc
On Fedora:
$ sudo dnf install cloc
On FreeBSD:
$ sudo pkg install cloc
It can also installed using third-party package manager like NPM as well.
$ npm install -g cloc
Count The Lines Of Source Code In Many Programming Languages
Let us start with a simple example. I have a "hello world" program written in C in my current working directory.
$ cat hello.c #include <stdio.h> int main() { // printf() displays the string inside quotation printf("Hello, World!"); return 0; }
To count the lines of code in the hello.c program, simply run:
$ cloc hello.c
Sample output:
The first column specifies the name of programming languages that the source code consists of. As you can see in the above output, the source code of "hello world" program is written using C programming language.
The second column displays the number of files in each programming languages. So, our code contains 1 file in total.
The third column displays the total number of blank lines. We have zero blank files in our code.
The fourth column displays number of comment lines.
And the final and fifth column displays total physical lines of given source code.
It is just a 6 line code program, so counting the lines in the code is not a big deal. What about the some big source code file? Have a look at the following example:
$ cloc file.tar.gz
Sample output:
As per the above output, it is quite difficult to manually find exact count of code. But, Cloc displays the result in seconds with nice tabular-column format. You can view the gross total of each section at the end which is quite handy when it comes to analyze the source code of a program.
Cloc not only counts the individual source code files, but also files inside directories and sub-directories, archives, and even in specific git commits etc.
Count the lines of codes in a directory:
$ cloc dir/
Sub-directory:
$ cloc dir/cloc/tests
Count the lines of codes in archive file:
$ cloc archive.zip
You can also count lines in a git repository, using a specific commit like below.
$ git clone https://github.com/AlDanial/cloc.git
$ cd cloc
$ cloc 157d706
Cloc can recognize several programming languages. To view the complete list of recognized languages, run:
$ cloc --show-lang
For more details, refer the help section.
$ cloc --help
Resource:
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!