惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
C
Check Point Blog
J
Java Code Geeks
腾讯CDC
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
罗磊的独立博客
Last Week in AI
Last Week in AI
B
Blog
IT之家
IT之家
S
SegmentFault 最新的问题
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
博客园 - 聂微东
U
Unit 42
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
MyScale Blog
MyScale Blog

博客园_首页

Plist 二进制格式 Milvus 和 PGVector,哪个更好? OpenClaw 已过时?在 VS Code 中运行 Hermes Agent! 第30篇文章:一个大三计科生的自白 Manim如何在数学公式中完美显示中文? Docker 部署 RocketMQ 5 并发编程核心概念辨析 C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生 CLI 是什么?为什么大厂突然集体卷命令行? 【从0到1构建一个ClaudeAgent】协作-自主Agent UIImageView 设置图片不生效的原因排查 最小二乘问题详解20:无先验约束下的增量式SFM自由网平差 痞子衡嵌入式:大话双核i.MXRT1180之XIP应用里借助MU实现可靠Flash IAP的方法 AI Chat 封装, SemanticKerne.AiProvider.Unified 已发布 Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量 在后台服务中使用 Scoped 服务,为什么总是报错? H200 安装驱动并使用sglang启动模型 wireshark 抓包Trap上报告警内容 我用 AI 辅助开发了一系列小工具(2):图片压缩工具 [A Primer On MC and CC] 2.1 Memory Consistency 1 - 指令重排序和 SC 模型 Oracle数据库SCN推进技术详解与实践指南 玩转控件:封装个带图片的Label控件 Claude Code 4.7 真正该升级的不是模型,而是你的工作流 前端小白一句话,AI 帮我做了个颜值拉满的桌面媒体播放器。当代码不再是门槛,一句话编程就是现实。 5. WorkBuddy: 小龙虾的灵魂三件套,让你的小龙虾不只是工具 SQLite 分片方案实战:三种分片策略的深度对比 告别简陋 UI!一款基于 Fluent Design 和基于 WinUI 的开源免费、现代化的 Avalonia UI 控件库 关于二进制排列组合枚举的总结 AI开发-python-LangGraph框架(3-27-LangGraph从零实现大模型智能决策工作流) ElasticSearch主分片和副本分片概念详解
代码行统计工具
wenha · 2026-05-07 · via 博客园_首页

cloc(Count Lines of Code) 是一个跨平台、开源的代码行统计工具,用 Perl 编写,可统计空行、注释行、有效代码行,支持超 200 种编程语言,能递归扫描目录、排除指定文件夹/后缀、按语言分类输出。


一、安装(Windows/Linux/macOS)

Windows(3种方式)

  1. Chocolatey(推荐)
choco install cloc
  1. Scoop
scoop install cloc
  1. 手动下载(便携版,无依赖)

Linux

# Ubuntu/Debian
sudo apt update && sudo apt install cloc

# CentOS/RHEL 8+
sudo dnf install cloc

# Fedora
sudo dnf install cloc

macOS

# Homebrew(推荐)
brew install cloc

# MacPorts
sudo port install cloc

验证安装

cloc --version

二、核心用法(高频命令)

1. 统计当前目录(递归)

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)

cloc . --exclude-dir=node_modules,dist,build,venv,target

3. 排除文件后缀

cloc . --exclude-ext=log,tmp,md

4. 只统计指定语言

cloc . --include-lang=Python,JavaScript,Java

5. 按文件明细输出

cloc src/ --by-file

6. 输出 CSV/JSON(用于报表)

# CSV
cloc . --csv > report.csv

# JSON
cloc . --json > report.json

7. 只统计 Git 跟踪文件(忽略未提交文件)

cloc . --vcs=git

三、常见问题与避坑

  1. 不要用 pip install cloc:这是同名 Python 包,非官方 cloc,装了无法运行。
  2. Windows 中文路径乱码:运行前设置环境变量:
$env:PERL_UNICODE="S"
  1. 统计耗时过长:必加 --exclude-dir=node_modules,target,否则大型项目会卡死。
  2. 如何查看支持的语言
cloc --show-lang

四、总结

cloc 是零依赖、跨平台、高精度的代码统计神器,核心命令就 3 个:安装→基础统计→排除目录。前端/后端/移动端项目通用,适合做代码规模评估、技术债治理、团队绩效参考。