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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - wenha

Linux之SSH远程登录 大模型基础(六):从聊天机器人到智能体-大模型的下一站 大模型基础(五):RAG入门-让大模型学会"开卷考试" 大模型基础(四):强化学习入门-从斯金纳箱到大模型推理 大模型基础(三):大模型是怎么炼成的-从预训练到强化学习的完整流程 数据库隔离级别 踩坑记录:UTF-8、UTF-8-BOM 与 GB2312 读取的乱码真相 大模型基础(二):必懂5大基础概念《Token、上下文窗口、Embedding、预训练、微调》 大模型基础(一):什么是LLM? 告别玄学调参:Agent 稳定落地的核心——Harness Engineering 体验优先:十分钟使用 Python+LangChain 玩转阿里通义千问 运维工具之Ansible详解(一) 运维工具之Ansible详解(二) 自建K8S集群认证过期 单点登录的设计与实现思路 (三)Dockerfile基础 (二)Docker镜像、容器、数据卷 (一)Docker简介 Go语言学习笔记(四) Go语言学习笔记(三) Redis学习笔记(一)配置文件参数说明 Go语言学习笔记(二) CentOS上安装RabbitMQ CentOS上安装Redis NodeJS学习笔记(一) JavaScript的发展史 用户中心 - 博客园
代码行统计工具
wenha · 2026-05-07 · via 博客园 - wenha

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 个:安装→基础统计→排除目录。前端/后端/移动端项目通用,适合做代码规模评估、技术债治理、团队绩效参考。