Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
cloc – Count Lines of Code in Many Programming Languages
cloc (Count Lines of Code) is a free, open-source command-line tool that counts blank lines, comment lines, and physical lines of source code in many programming languages. It supports over 200 programming languages and can analyze individual files, directories, or compressed archives, making it an essential tool for developers and project managers to measure codebase complexity.
The tool is cross-platform, available for Windows, Linux, and macOS, and can be easily integrated into development workflows for automated code metrics collection.
Installation
Installing cloc varies by operating system. On Linux distributions, use your package manager
Ubuntu/Debian
sudo apt-get install cloc
CentOS/RHEL
sudo yum install cloc
Arch Linux
sudo pacman -S cloc
macOS (using Homebrew)
brew install cloc
Windows (using Chocolatey)
choco install cloc
Basic Usage
The basic syntax for cloc is
cloc [options] [files/directories]
To count lines of code in the current directory and all subdirectories
cloc .
This produces output organized by programming language
------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------
Common Options
cloc provides numerous options to customize analysis and output format
| Option | Description | Example |
|---|---|---|
--exclude-dir |
Exclude specific directories | cloc --exclude-dir=node_modules,dist . |
--exclude-ext |
Exclude file extensions | cloc --exclude-ext=log,tmp . |
--include-lang |
Count only specified languages | cloc --include-lang=Python,JavaScript . |
--by-file |
Show results by individual file | cloc --by-file src/ |
--csv |
Output in CSV format | cloc --csv . > report.csv |
--quiet |
Show only summary totals | cloc --quiet . |
Practical Examples
Analyzing a Web Project
cloc --exclude-dir=node_modules,build --include-lang=JavaScript,TypeScript,CSS,HTML .
Generating Reports
# Generate CSV report cloc --csv --out=project_metrics.csv src/ # Generate XML report cloc --xml --out=project_metrics.xml src/ # Compare two versions cloc --diff version1/ version2/
Analyzing Compressed Files
cloc project.tar.gz cloc source_code.zip
Advanced Features
cloc offers advanced capabilities for detailed analysis
Language Detection Automatically recognizes file types based on extensions and content
Custom Definitions Define custom languages using
--force-langoptionProgress Indicators Use
--progress-ratefor large codebasesDiff Analysis Compare codebases between versions using
--diffMulti-threading Speed up analysis with
--processes=N
Output Formats
cloc supports multiple output formats for integration with other tools
# JSON format for programmatic processing cloc --json . # YAML format cloc --yaml . # SQL format for database insertion cloc --sql project_metrics .
Integration Tips
Integrate cloc into development workflows
CI/CD Pipelines Add cloc commands to track code growth over time
Git Hooks Run cloc in pre-commit hooks to monitor code changes
Documentation Include cloc reports in project documentation
Code Reviews Use cloc to understand the scope of proposed changes
Conclusion
cloc is a powerful and versatile tool for measuring codebase size and complexity across hundreds of programming languages. Its extensive customization options, multiple output formats, and cross-platform availability make it invaluable for developers, project managers, and DevOps teams. By incorporating cloc into your development workflow, you can track code growth, assess project complexity, and make data-driven decisions about code quality and maintainability.
