LinuxCommandLibrary

cloc

source code line counter

TLDR

Count lines in directory

$ cloc [directory]
copy
Count lines in file
$ cloc [file.cpp]
copy
Count with file-by-file report
$ cloc --by-file [directory]
copy
Count in archive
$ cloc [archive.tar.gz]
copy
Compare two versions
$ cloc --diff [old/] [new/]
copy
Output as CSV
$ cloc --csv [directory]
copy
Output as JSON
$ cloc --json [directory]
copy
Exclude directory
$ cloc --exclude-dir=[test,vendor] [directory]
copy
List supported languages
$ cloc --show-lang
copy

SYNOPSIS

cloc [options] file|directory|archive...

DESCRIPTION

cloc (Count Lines of Code) counts blank lines, comment lines, and physical lines of source code in over 200 programming languages. It analyzes individual files, entire directory trees, and compressed archives, producing summary reports broken down by language or by file.
The tool includes a diff mode that compares two codebases and reports added, removed, modified, and unchanged lines. Output can be generated in plain text, CSV, JSON, SQL, or XML format for integration with other tools and reporting systems.
Written in Perl with no external dependencies, cloc is easy to install and runs on any platform with a Perl interpreter. It correctly handles multi-language files and uses language-specific comment detection rules for accurate counting.

PARAMETERS

--by-file

Report results per source file
--by-file-by-lang
Report per file and per language
--diff
Compare two code bases
--csv
Output in CSV format
--json
Output in JSON format
--sql=file
Output as SQL insert statements
--exclude-dir=list
Comma-separated directories to exclude
--exclude-lang=list
Comma-separated languages to exclude
--include-lang=list
Only count specified languages
--max-file-size=MB
Skip files larger than MB (default: 100)
--show-lang
List recognized languages
--quiet
Suppress progress output

OUTPUT FIELDS

Language, files, blank lines, comment lines, code lines

DIFF MODE

$ cloc --diff old_version/ new_version/
copy
Shows added, removed, modified, and same lines between versions.

CAVEATS

Does not analyze semantic code complexity. Some edge cases in multi-language files may be miscounted. Supports 200+ languages.

SEE ALSO

wc(1), sloccount(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community