How to Install and Use cloc in Linux Terminal to Count Lines of Code and Comments

Auditing the scale and composition of a software project is essential when planning migrations, assessing technical debt, or calculating development costs. Running standard word-count utilities like wc -l counts every single newline character, including blank formatting lines, licensing headers, and documentation comments. To obtain accurate metrics on actual source code, developers rely on specialized code analyzers.

cloc (Count Lines of Code) is the industry-standard open-source command-line utility for inspecting software repositories. It automatically detects hundreds of programming languages, separates physical lines of executable source code from blank lines and comments, and can even process compressed archive files and Git commit diffs directly. While modern compiled utilities allow you to count lines of code with tokei or calculate code complexity with scc, cloc remains the most widely packaged and battle-tested standard across Linux distributions.

Installing cloc on Linux

Because cloc is written in self-contained Perl without complex external binary dependencies, it is available in official repositories across almost every Linux distribution.

Installing on Ubuntu, Debian, and Linux Mint

sudo apt update
sudo apt install cloc

Installing on Fedora, RHEL, and CentOS

sudo dnf install cloc

Installing on Arch Linux and Manjaro

sudo pacman -S cloc

Installing on openSUSE

sudo zypper install cloc

How to Use cloc in Linux Terminal

Using cloc is straightforward and requires minimal configuration for standard project directories.

1. Counting Lines in a Directory

To analyze all source files within your current working directory, run cloc followed by a period or directory path:

cloc .

The output generates an easy-to-read ASCII table broken down by programming language:

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
TypeScript                     142           2410           1890          14820
HTML                            18            180             45           2310
CSS                             12            210             80           1950
JSON                            24              0              0           1240
Markdown                        15            320              0            890
-------------------------------------------------------------------------------
SUM:                           211           3120           2015          21210
-------------------------------------------------------------------------------

2. Excluding Vendor and Cache Folders

Modern projects typically contain external dependencies (such as node_modules, vendor, dist, or virtual environments) that artificially inflate line counts. Use the --exclude-dir flag to omit them:

cloc --exclude-dir=node_modules,dist,build,vendor .

3. Analyzing Specific File Types

If you only want metrics for specific programming languages, use the --include-lang flag:

cloc --include-lang=Python,SQL ./backend

4. Counting Code Inside Compressed Archives

A standout capability of cloc is its ability to inspect compressed archives without requiring manual extraction. You can pass tarballs or zip archives directly:

cloc linux-6.12.tar.xz

The utility temporarily unpacks the archive into memory, categorises the source files, and displays the report.

5. Comparing Code Diffs Between Git Commits

You can use cloc to audit how many lines of actual code (excluding comments) were added, deleted, or modified between two Git branches or commits:

cloc --git --diff main feature-branch

This report highlights the exact net code change introduced during a pull request or sprint cycle.

Exporting cloc Reports to JSON, CSV, and XML

If you are integrating cloc into continuous integration (CI/CD) pipelines or automated code-review dashboards, export the results in structured formats:

  • Export to JSON:
    cloc --json --out=code-report.json .
  • Export to CSV:
    cloc --csv --out=code-report.csv .
  • Export to XML:
    cloc --xml --out=code-report.xml .

cloc Command Options Quick Reference

Command Option Description
--by-file Displays metrics for each individual file instead of aggregating by language
--exclude-dir=DIR1,DIR2 Ignores specified directory names (e.g., test directories, dependencies)
--include-lang=LANG Restricts analysis to specified language(s)
--diff FILE1 FILE2 Calculates statistical differences between two directories or archives
--show-lang Prints all recognized programming languages and file extensions

Frequently Asked Questions

How does cloc distinguish comments from code?

cloc maintains extensive language grammar definition tables that recognize single-line comments (like // or #), multi-line docstrings (like /* ... */ or """ ... """), and embedded HTML/CSS tags, preventing comments from inflating physical code counts.

Can cloc handle custom file extensions?

Yes. If your team uses non-standard extensions (for example, mapping .inc to PHP), define the mapping using the --force-lang argument:

cloc --force-lang=PHP,inc .

Is cloc suitable for massive multi-gigabyte mono-repositories?

While cloc handles hundreds of thousands of files reliably, it runs as a single Perl process. For massive mono-repos where scanning speed is paramount, multithreaded alternatives like tokei or scc execute faster, while cloc excels at precision, language customization, and archive parsing.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.