cloc – Count Lines of Code in Many Programming Languages


As a developer, one of the key metrics for measuring the complexity and size of a software project is the number of lines of code (LOC). Counting LOC is a tedious and time-consuming task that is prone to errors, especially in large codebases. Fortunately, there's a tool called cloc that can automate the process and provide accurate and detailed information on the number of lines of code in many programming languages.

cloc is a free and open-source tool that can count the lines of code in over 200 programming languages. It's available for Windows, Linux, and macOS, and can be used from the command line or integrated into your development workflow. In this article, we'll explore the features and capabilities of cloc, as well as provide a tutorial on how to use it to count lines of code in your projects.

Getting Started with cloc

Before we dive into the details of using cloc, let's start by installing it on our system. The installation process for cloc varies based on the operating system you use. For now, let’s look at how to install it on a Linux system.

Installing cloc on Linux

For Linux distributions, cloc can be installed using the package manager. Depending on the distribution, the package manager may be apt for Debian/Ubuntu, yum for CentOS/RHEL, or pacman for Arch Linux. To install cloc, open a terminal window and run the following command −

sudo apt-get install cloc

or

sudo yum install cloc

or

sudo pacman -S cloc

If cloc is not available in the default repositories, you may need to add an additional repository or download and install the package manually.

Once you've installed cloc, you can start using it to count lines of code in your projects. To get started, open a terminal or command prompt and navigate to the directory containing the code you want to analyze. Then, run the following command −

cloc .

This will instruct cloc to count the lines of code in the current directory and all its subdirectories. By default, cloc will display the results in the terminal, organized by programming language. For example, the output might look something like this −

------------------------------------------------------------------------------- 
Language files blank comment code -------------------------------------------------
------------------------------ Python 21 416 710 1823 HTML 32 157 36 1437 CSS 9 59 
0 483 JavaScript 14 106 174 427 ---------------------------------------------------
---------------------------- SUM: 76 738 920 4170 ---------------------------------
----------------------------------------------

This output shows the number of files, blank lines, comment lines, and code lines for each programming language. It also includes a summary of the total lines of code in the project.

Using cloc Options

cloc provides several options for customizing the output and behavior of the tool. For example, you can use the --exclude-dir option to exclude certain directories from the analysis, or the --csv option to output the results in CSV format for further analysis.

Here are some examples of useful cloc options −

  • --exclude-dir=dir1,dir2 − Exclude the directories dir1 and dir2 from the analysis.

  • --exclude-ext=ext1,ext2 − Exclude files with the extensions ext1 and ext2 from the analysis.

  • --ignore-case − Ignore case when counting lines of code.

  • --quiet − Suppress the output of individual file counts and only display the summary.

  • --by-file − Display the results by individual file instead of by programming language.

For a full list of cloc options, you can use the --help option:

cloc --help

Let's move on to how to use cloc to count lines of code in different programming languages.

Using cloc

cloc can be installed on various platforms, including Linux, macOS, and Windows. It's also available as a Docker image. Once you have installed cloc, you can use it to count the lines of code in your project.

Here's a basic syntax for using cloc.

cloc [options] [file/dir(s)]

You can provide options to customize the output of the command. For example, you can use the --exclude-dir option to exclude directories from being counted. Here's an example −

cloc --exclude-dir=node_modules .

This command counts the lines of code in the current directory while excluding the node_modules directory.

By default, cloc counts lines of code in over 200 programming languages. You can also specify the languages you want to count by using the --include-lang option. For example, the following command counts only the lines of code in Python and JavaScript files −

cloc --include-lang=Python,JavaScript .

You can also use the --csv option to output the result as a CSV file. This is useful if you want to process the output using a script. For example −

cloc --csv . > result.csv

This command counts the lines of code in the current directory and saves the result as a CSV file named result.csv.

There are many more options available for cloc. You can check the documentation for a complete list.

Conclusion

In this article, we've looked at cloc, a command-line tool for counting lines of code in many programming languages. We've seen how to install cloc on Linux and how to use it to count lines of code in a directory, file, or compressed archive. We've also looked at some of the various options for customizing cloc's behavior, such as specifying which languages to count and outputting the results in different formats.

cloc is a powerful tool that can help developers and project managers better understand the size and complexity of their codebases. By using cloc, you can quickly and easily get an accurate count of the lines of code in your project, which can help you make informed decisions about code quality, maintainability, and productivity. So if you haven't already, give cloc a try and see how it can help you better understand your code.

Updated on: 23-Jun-2023

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements