Linux command
cloc 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Count lines in directory
cloc [directory]
Count lines in file
cloc [file.cpp]
Count with file-by-file report
cloc --by-file [directory]
Count in archive
cloc [archive.tar.gz]
Compare two versions
cloc --diff [old/] [new/]
Output as CSV
cloc --csv [directory]
Output as JSON
cloc --json [directory]
Exclude directory
cloc --exclude-dir=[test,vendor] [directory]
List supported languages
cloc --show-lang
说明
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 module 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. It can also operate on git repositories directly, comparing branches or commits.
参数
- --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
- --include-ext=_list_
- Only count files with the given comma-separated extensions.
- --max-file-size=_MB_
- Skip files larger than _MB_ megabytes (default: 100).
- --show-lang
- List recognized languages and exit.
- --git
- Forces inputs to be interpreted as git targets (commit hashes, branch names).
- --vcs=_VCS_
- Use _VCS_ to obtain list of files to count (e.g., git, svn).
- --xml
- Output in XML format.
- --yaml
- Output in YAML format.
- --quiet
- Suppress progress output.
- --force-lang=_LANG_
- Process all files with the given language counter.
- --config _FILE_
- Read command-line switches from _FILE_ instead of defaults.
FAQ
What is the cloc command used for?
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 module 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. It can also operate on git repositories directly, comparing branches or commits.
How do I run a basic cloc example?
Run `cloc [directory]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --by-file do in cloc?
Report results per source file