LinuxCommandLibrary

cloc

Count lines of code in files

TLDR

Count all the lines of code in a directory

$ cloc [path/to/directory]
copy

Compare 2 directory structures and count the differences between them
$ cloc --diff [path/to/directory1] [path/to/directory2]
copy

Ignore files that are ignored by VCS, such as files specified in .gitignore
$ cloc --vcs git [path/to/directory]
copy

Display the results for each file instead of each language
$ cloc --by-file [path/to/directory]
copy

SYNOPSIS

cloc [OPTIONS] [FILE|DIRECTORY]...

PARAMETERS

--help
    Print usage information and exit

--version
    Print the version and exit

--list-languages
    List all supported languages

--show-lang, -l
    Print language processing order

--exclude-dir=NAME
    Exclude directories matching NAME (can repeat)

--exclude-lang=LANG
    Exclude files of given language

--include-lang=LANG
    Only count files of given language

--exclude-ext=EXT
    Exclude files with given extension

--match-d=DIR
    Only count directories matching regex

--not-match-d=DIR
    Exclude directories matching regex

--by-file
    Report results for every source file

--csv
    CSV format output

--json
    JSON format output

--out=FILE
    Output to FILE instead of stdout

--progress-rate=N
    Show progress update every N files

--diff A B
    Compare code in A and B

--strip-comments=EXT
    Remove comments before counting

--quiet
    Suppress all messages except warnings

DESCRIPTION

cloc (Count Lines of Code) is a command-line tool that analyzes source code files and directories to count physical lines of code, blank lines, and comment lines across over 400 programming languages and markup formats. It recurses into subdirectories by default, providing a summary report with totals and breakdowns by file or language. Features include customizable language definitions, exclusion of directories/files/patterns, multiple output formats (plain text, CSV, JSON, XML, YAML, SQLite), progress indicators, and options for stripping comments or matching files by patterns.

Ideal for software project metrics, code reviews, and license compliance checks, cloc ignores binary files and generated code when configured. It supports --diff for comparing two versions of a codebase, highlighting changes in line counts. Highly accurate due to language-specific parsers, outperforming simple line counters like wc. Written in Perl, it's lightweight, portable, and actively maintained.

CAVEATS

Not installed by default; requires Perl. May misclassify files with unusual extensions. Large codebases can be slow without exclusions.

INSTALLATION

On Debian/Ubuntu: sudo apt install cloc
On macOS: brew install cloc
Or download from GitHub.

BASIC EXAMPLE

cloc . counts code in current directory.
cloc src/ --exclude-dir=.git,node_modules skips common dirs.

HISTORY

Developed by Al Danial starting in 2006 as a faster, more accurate Perl replacement for sloccount. Reached version 1.0 in 2010; now at 2.00+ with GitHub repo, supporting hundreds of languages via community contributions. Widely used in open-source projects for SLOC metrics.

SEE ALSO

wc(1), tokei(1), sloccount(1)

Copied to clipboard