LinuxCommandLibrary

loc

Find files by name

TLDR

Print lines of code in the current directory

$ loc
copy

Print lines of code in the target directory
$ loc [path/to/directory]
copy

Print lines of code with stats for individual files
$ loc --files
copy

Print lines of code without .gitignore (etc.) files (e.g. two -u flags will additionally count hidden files and dirs)
$ loc [[-u|--unrestricted]]
copy

SYNOPSIS

cloc [options]

PARAMETERS

--help
    Display help message with available options.

--version
    Show program version information.

--by-file
    Report results for each file processed, not just totals.

--by-file-by-lang
    Report results for each file, organized by programming language.

--exclude-dir=
    Exclude specified directories from the analysis.

--exclude-ext=
    Exclude files with the specified extensions.

--include-lang=
    Only count lines for the specified languages.

--csv
    Output results in comma-separated values (CSV) format.

--json
    Output results in JSON (JavaScript Object Notation) format.

--xml
    Output results in XML (Extensible Markup Language) format.

--sum-one-file
    Treat single files (e.g., polyglot files) as distinct languages in the summary.

--force-lang=,
    Force files with specified extensions to be counted as a certain language.

--skip-unrecognized
    Skip files whose language cloc cannot identify.

--vcs=
    Process files from a specified version control system (e.g., git, svn).

--git
    Shortcut for --vcs=git, analyzing files from a Git repository.

--ignored=
    Save a list of all ignored files to the specified file.

DESCRIPTION

Disclaimer: The command 'loc' is not a standard Unix/Linux command. This analysis assumes 'loc' is an abbreviation or common reference to cloc (Count Lines Of Code), a widely used command-line utility for counting lines of code, blank lines, and comments.

cloc is a free, open-source tool designed to process various file types, including source code files, header files, and configuration files, providing a summary for each language detected and an overall total. It's extensively used by developers and project managers for estimating project size, tracking project progress, and analyzing codebases. The tool supports hundreds of programming languages and can analyze code in local directories, archives (like zip, tar), and even directly from version control repositories like Git and Subversion. Its versatility and accurate language parsing make it an indispensable tool for code metrics.

CAVEATS

cloc relies on file extensions and internal heuristics to identify languages, which might not always be accurate for all edge cases or highly custom file types. It primarily counts physical lines of code, blank lines, and comments; it does not parse code for logical units or functional complexity. Performance can vary significantly based on the number and size of files being analyzed, and processing large codebases can be time-consuming.

LANGUAGE RECOGNITION

cloc employs a robust internal database of language definitions and regular expressions to accurately identify over 200 programming languages, scripting languages, markup languages, and other text-based formats. This sophisticated mechanism allows it to precisely distinguish between executable code, comment lines, and blank lines, adhering to each language's specific syntax rules.

OUTPUT FORMATS

Beyond its clear, human-readable console output, cloc provides various machine-readable output formats, including CSV, JSON, and XML. This flexibility makes it exceptionally easy to integrate into automated workflows, continuous integration/continuous deployment (CI/CD) pipelines, custom reporting tools, and other systems for generating metrics and dashboards programmatically.

HISTORY

cloc was originally developed by Al Danial and first released around 2006. Written in Perl, it rapidly gained popularity due to its extensive language support, impressive processing speed, and cross-platform compatibility. It has since been actively maintained and continuously improved, evolving into a de-facto standard tool within the software development community for generating code metrics and analyzing codebase size.

SEE ALSO

cloc(1), sloccount(1), wc(1), find(1), grep(1)

Copied to clipboard