cloc(Count Lines of Code) 是一个跨平台、开源的代码行统计工具,用 Perl 编写,可统计空行、注释行、有效代码行,支持超 200 种编程语言,能递归扫描目录、排除指定文件夹/后缀、按语言分类输出。
一、安装(Windows/Linux/macOS)
Windows(3种方式)
- Chocolatey(推荐)
powershell
choco install cloc
- Scoop
powershell
scoop install cloc
- 手动下载(便携版,无依赖)
- Releases 页下载
cloc-x.x.x.zip:https://github.com/AlDanial/cloc/releases - 解压后把
cloc.exe加入环境变量 PATH。

Linux
bash
# Ubuntu/Debian
sudo apt update && sudo apt install cloc
# CentOS/RHEL 8+
sudo dnf install cloc
# Fedora
sudo dnf install cloc
macOS
bash
# Homebrew(推荐)
brew install cloc
# MacPorts
sudo port install cloc
验证安装
bash
cloc --version
二、核心用法(高频命令)
1. 统计当前目录(递归)
bash
cloc .
输出示例:
D:\Code\GitSrc>cloc-2.08.exe .
61537 text files.
59345 unique files.
10905 files ignored.
github.com/AlDanial/cloc v 2.08 T=440.78 s (134.6 files/s, 38456.0 lines/s)
----------------------------------------------------------------------------------------
Language files blank comment code
----------------------------------------------------------------------------------------
C# 32645 859094 1614305 6114931
SQL 14911 500764 334907 3632991
JavaScript 5490 165789 222592 1627784
XML 966 7638 20968 459256
JSON 274 80 0 387787
HTML 1579 23698 13296 275471
CSS 400 17510 5224 140375
MSBuild script 981 317 766 76952
TypeScript 419 5831 11806 74321
LESS 511 8761 3004 67279
C# Designer 241 4439 17494 38356
C++ 95 7586 5472 35740
YAML 3 7164 0 27020
Text 106 354 0 18439
Properties 105 1648 5637 10708
SCSS 119 654 197 8806
C/C++ Header 107 2105 2466 8491
Visual Studio Solution 44 43 44 6723
SVG 54 3284 36 5878
JSX 36 495 338 5750
Ruby 1 3226 0 5242
ASP.NET 17 450 105 4496
Markdown 46 1419 1 4200
C# Generated 7 1485 2390 3711
Web Services Description 4 0 0 3426
Windows Resource File 6 303 234 3190
vim script 4 143 245 1961
Visual Basic .NET 6 165 184 1719
Python 14 388 1209 1443
IDL 12 159 0 1367
XSD 3 8 0 1037
Razor 12 186 266 955
ASP 8 155 0 804
Velocity Template Language 14 46 73 611
DOS Batch 45 159 53 562
INI 11 19 0 522
diff 13 40 308 491
VBScript 3 51 35 443
XSLT 2 32 2 320
PowerShell 7 80 60 281
Bourne Shell 12 56 38 247
EJS 1 14 0 142
TNSDL 2 0 0 87
Thrift 1 16 414 65
Perl 2 26 33 47
NAnt script 2 0 14 46
C 1 26 14 39
Windows Module Definition 2 4 3 17
PHP 1 0 0 3
----------------------------------------------------------------------------------------
SUM: 59345 1625910 2264233 13060532
----------------------------------------------------------------------------------------
One or more files took longer to process than expected.
Try rerunning without timeout guards by adding --timeout=0
to your command line arguments. See the documentation on
the --timeout switch for more information.
2. 排除目录(必加!如 node_modules、dist)
bash
cloc . --exclude-dir=node_modules,dist,build,venv,target
3. 排除文件后缀
bash
cloc . --exclude-ext=log,tmp,md
4. 只统计指定语言
bash
cloc . --include-lang=Python,JavaScript,Java
5. 按文件明细输出
bash
cloc src/ --by-file
6. 输出 CSV/JSON(用于报表)
bash
# CSV
cloc . --csv > report.csv
# JSON
cloc . --json > report.json
7. 只统计 Git 跟踪文件(忽略未提交文件)
bash
cloc . --vcs=git
三、常见问题与避坑
- 不要用 pip install cloc:这是同名 Python 包,非官方 cloc,装了无法运行。
- Windows 中文路径乱码:运行前设置环境变量:
powershell
$env:PERL_UNICODE="S"
- 统计耗时过长 :必加
--exclude-dir=node_modules,target,否则大型项目会卡死。 - 如何查看支持的语言:
bash
cloc --show-lang
四、总结
cloc 是零依赖、跨平台、高精度 的代码统计神器,核心命令就 3 个:安装→基础统计→排除目录。前端/后端/移动端项目通用,适合做代码规模评估、技术债治理、团队绩效参考。