Open In App

CLOC – Count number of lines of code in file

Improve
Improve
Like Article
Like
Save
Share
Report

cloc is a command-line-based tool. It counts the blank lines, comment lines, actual code lines written in many programming languages. cloc is completely written in the Perl language, and it has no external dependencies. cloc can run on many operating systems like Linux, macOS, Windows, OpenBSD and many more.

Installation of cloc 

Now let’s see how we can install the cloc on different operating systems. Use one of the following commands according to your operating system:

For Debian, Ubuntu:

sudo apt install cloc          

For Red Hat, Fedora:

sudo yum install cloc            

Fedora 22 or later:

sudo dnf install cloc            

For Arch:

sudo pacman -S cloc              

For Gentoo :

sudo emerge -av dev-util/cloc    

For  Alpine Linux

sudo apk add cloc                

For OpenBSD

doas pkg_add cloc                

For  FreeBSD:

sudo pkg install cloc            

For macOS with MacPorts:

sudo port install cloc           

For  macOS with Homebrew:

brew install cloc                

For  Windows with Chocolatey:

choco install cloc               

For Windows with Scoop:

scoop install cloc               

To install the cloc using npm use the following command:

npm install -g cloc              

Now we have installed the cloc on the system. Let’s see how to use the cloc. The general syntax of using cloc is as follows

cloc [options] <FILE|DIR> ...

Now let’s see understand by one example. We have one source file written in CPP and contains the following code:

// hello.C
#include <iostream>
int main ()
{
std::cout << "hello" << std::endl;  // comment 1
std::cout << "again" << std::endl;  /* comment
2 */
}

Now let’s use the cloc to count the line

As we can see, the cloc has correctly counted the comments and code lines.

Using cloc on compressed file

cloc can also count the number of lines physical, commented, an empty line written in different languages in compressed files

cloc compressed_file

Let’s see one example, we have one compressed file which contains some files written in JS and markdown, Let’s use the cloc on that compressed file.

Get count of each file

To get the line of code count file-wise in folder or zip file use the –by-file option with the cloc command.

cloc --by-file folder/compressedFile

Counting lines in GitHub repository

cloc can also count the codes written in different files in the GitHub repository. Now let’s clone cloc GitHub repository. To count the number of lines in the GitHub repo the following command:

cloc commit

First, clone the repository and use the cloc.

git clone https://github.com/AlDanial/cloc
cd cloc
cloc ec44eb0

cloc provide many other options. To know more about the cloc read the man page of cloc.

man cloc


Last Updated : 23 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads