Home Command line utilities Cloc – Count The Lines Of Source Code In Many Programming Languages
Cloc - count the lines of source code

Cloc – Count The Lines Of Source Code In Many Programming Languages

By sk

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.


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:

Hello World Program

Cunt the lines in Hello World Program Using Cloc

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:

cloc 1

Count lines in a tar file

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/

cloc 2


$ cloc dir/cloc/tests

cloc 4

Count the lines of codes in archive file:

$ cloc archive.zip

cloc 3

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 5

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


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More