我们有一个PHP应用程序,并希望计算特定目录及其子目录下的所有代码行.我们不需要忽略评论,因为我们只是想弄清楚.
wc -l *.php
Run Code Online (Sandbox Code Playgroud)
该命令在给定目录中运行良好,但忽略子目录.我当时认为这可行,但它正在返回74,绝对不是这样......
find . -name '*.php' | wc -l
Run Code Online (Sandbox Code Playgroud)
提供所有文件的正确语法是什么?
小智 2532
尝试:
find . -name '*.php' | xargs wc -l
Run Code Online (Sandbox Code Playgroud)
SLOCCount工具也可能有所帮助.
它将为您指定的任何层次结构提供准确的源代码行数,以及一些其他统计信息.
Shi*_*zmo 456
对于另一个单线:
( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l
Run Code Online (Sandbox Code Playgroud)
适用于带空格的名称,只输出一个数字.
Mic*_*ild 388
如果使用最新版本的Bash(或ZSH),它会更简单:
wc -l **/*.php
Run Code Online (Sandbox Code Playgroud)
在Bash shell中,这需要设置globstar
选项,否则**
glob-operator不是递归的.要启用此设置,请发出
shopt -s globstar
Run Code Online (Sandbox Code Playgroud)
为了使这个永久性的,它添加到初始化文件之一(~/.bashrc
,~/.bash_profile
等等).
sim*_*mao 327
您可以使用cloc
为此目的而构建的实用程序.它会报告每种语言的每行数量,以及其中有多少是评论等.CLOC可在Linux,Mac和Windows上使用.
用法和输出示例:
$ cloc --exclude-lang=DTD,Lua,make,Python .
2570 text files.
2200 unique files.
8654 files ignored.
http://cloc.sourceforge.net v 1.53 T=8.0 s (202.4 files/s, 99198.6 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Javascript 1506 77848 212000 366495
CSS 56 9671 20147 87695
HTML 51 1409 151 7480
XML 6 3088 1383 6222
-------------------------------------------------------------------------------
SUM: 1619 92016 233681 467892
-------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
Cal*_*ius 95
在类UNIX系统上,有一个名为cloc
提供代码统计信息的工具.
我在代码库中的一个随机目录中运行它说:
59 text files.
56 unique files.
5 files ignored.
http://cloc.sourceforge.net v 1.53 T=0.5 s (108.0 files/s, 50180.0 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 36 3060 1431 16359
C/C++ Header 16 689 393 3032
make 1 17 9 54
Teamcenter def 1 10 0 36
-------------------------------------------------------------------------------
SUM: 54 3776 1833 19481
-------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
Paw*_*icz 34
您没有指定有多少文件或所需的输出.这是你想要的:
find . -name '*.php' | xargs wc -l
Run Code Online (Sandbox Code Playgroud)
Mot*_*tys 27
又一个变种:)
$ find . -name '*.php' | xargs cat | wc -l
Run Code Online (Sandbox Code Playgroud)
编辑:这将给出总和,而不是逐个文件.
jon*_*tan 25
令人惊讶的是,没有基于find -exec
和的答案awk
.开始了:
find . -type f -exec wc -l {} \; | awk '{ SUM += $0} END { print SUM }'
Run Code Online (Sandbox Code Playgroud)
此片段可查找所有文件(-type f
).要通过文件扩展名查找,请使用-name
:
find . -name '*.py' -exec wc -l '{}' \; | awk '{ SUM += $0; } END { print SUM; }'
Run Code Online (Sandbox Code Playgroud)
Pau*_*per 21
POSIX
与此处的大多数其他答案不同,这些答案适用于任何POSIX系统,任意数量的文件以及任何文件名(除非另有说明).
每个文件中的行:
find . -name '*.php' -type f -exec wc -l {} \;
# faster, but includes total at end if there are multiple files
find . -name '*.php' -type f -exec wc -l {} +
Run Code Online (Sandbox Code Playgroud)
每个文件中的行,按文件路径排序
find . -name '*.php' -type f | sort | xargs -L1 wc -l
# for files with spaces or newlines, use the non-standard sort -z
find . -name '*.php' -type f -print0 | sort -z | xargs -0 -L1 wc -l
Run Code Online (Sandbox Code Playgroud)
每个文件中的行,按行数排序,降序
find . -name '*.php' -type f -exec wc -l {} \; | sort -nr
# faster, but includes total at end if there are multiple files
find . -name '*.php' -type f -exec wc -l {} + | sort -nr
Run Code Online (Sandbox Code Playgroud)
所有文件中的总行数
find . -name '*.php' -type f -exec cat {} + | wc -l
Run Code Online (Sandbox Code Playgroud)
ser*_*ych 20
对我来说更常见和简单,假设您需要计算不同名称扩展名的文件(例如,也是本地人)
wc $(find . -type f | egrep "\.(h|c|cpp|php|cc)" )
Run Code Online (Sandbox Code Playgroud)
Joe*_*lis 16
Tokei工具显示有关目录中代码的统计信息。Tokei 将显示文件数、这些文件中的总行数以及按语言分组的代码、注释和空白。Tokei 也可在 Mac、Linux 和 Windows 上使用。
Tokei 的输出示例如下:
$ tokei
-------------------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-------------------------------------------------------------------------------
CSS 2 12 12 0 0
JavaScript 1 435 404 0 31
JSON 3 178 178 0 0
Markdown 1 9 9 0 0
Rust 10 408 259 84 65
TOML 3 69 41 17 11
YAML 1 30 25 0 5
-------------------------------------------------------------------------------
Total 21 1141 928 101 112
-------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
可以按照存储库中 README 文件上的说明安装 Tokei 。
enn*_*ler 13
你想要的是一个简单的for
循环:
total_count=0
for file in $(find . -name *.php -print)
do
count=$(wc -l $file)
let total_count+=count
done
echo "$total_count"
Run Code Online (Sandbox Code Playgroud)
小智 12
仅限来源:
wc `find`
Run Code Online (Sandbox Code Playgroud)
要过滤,只需使用grep
wc `find | grep .php$`
Run Code Online (Sandbox Code Playgroud)
gni*_*urf 11
一个直截了当的快速,将使用所有的搜索/过滤功能find
,当文件太多(数字参数溢出)时不会失败,与名称中有趣符号的文件一起正常工作,不使用xargs
,不会启动无益地高数量的外部命令的(由于+
为find
的-exec
).干得好:
find . -name '*.php' -type f -exec cat -- {} + | wc -l
Run Code Online (Sandbox Code Playgroud)
如果您想保持简单,请省去中间人并仅wc
使用所有文件名进行调用:
wc -l `find . -name "*.php"`
Run Code Online (Sandbox Code Playgroud)
或者用现代语法:
wc -l $(find . -name "*.php")
Run Code Online (Sandbox Code Playgroud)
只要任何目录名或文件名中没有空格,此操作就有效。只要您没有数以万计的文件(现代 shell 支持非常长的命令行)。您的项目有 74 个文件,因此您有足够的扩展空间。
bash
工具总是很好用,但为了这个目的,使用能够做到这一点的工具似乎更有效。截至 2022 年,我使用了一些主要的工具,即cloc (perl)、 gocloc (go)、pygount (python)。
无需过多调整即可获得各种结果。\n似乎最准确且速度极快的是gocloc
.
带有 vue 前端的小型 Laravel 项目示例:
\n$ ~/go/bin/gocloc /home/jmeyo/project/sequasa\n-------------------------------------------------------------------------------\nLanguage files blank comment code\n-------------------------------------------------------------------------------\nJSON 5 0 0 16800\nVue 96 1181 137 8993\nJavaScript 37 999 454 7604\nPHP 228 1493 2622 7290\nCSS 2 157 44 877\nSass 5 72 426 466\nXML 11 0 2 362\nMarkdown 2 45 0 111\nYAML 1 0 0 13\nPlain Text 1 0 0 2\n-------------------------------------------------------------------------------\nTOTAL 388 3947 3685 42518\n-------------------------------------------------------------------------------\n\n
Run Code Online (Sandbox Code Playgroud)\n$ cloc /home/jmeyo/project/sequasa\n 450 text files.\n 433 unique files. \n 40 files ignored.\n\ngithub.com/AlDanial/cloc v 1.90 T=0.24 s (1709.7 files/s, 211837.9 lines/s)\n-------------------------------------------------------------------------------\nLanguage files blank comment code\n-------------------------------------------------------------------------------\nJSON 5 0 0 16800\nVuejs Component 95 1181 370 8760\nJavaScript 37 999 371 7687\nPHP 180 1313 2600 5321\nBlade 48 180 187 1804\nSVG 27 0 0 1273\nCSS 2 157 44 877\nXML 12 0 2 522\nSass 5 72 418 474\nMarkdown 2 45 0 111\nYAML 4 11 37 53\n-------------------------------------------------------------------------------\nSUM: 417 3958 4029 43682\n-------------------------------------------------------------------------------\n\n
Run Code Online (Sandbox Code Playgroud)\n$ pygount --format=summary /home/jmeyo/project/sequasa\n\xe2\x94\x8f\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb3\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb3\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb3\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb3\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb3\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb3\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x93\n\xe2\x94\x83 Language \xe2\x94\x83 Files \xe2\x94\x83 % \xe2\x94\x83 Code \xe2\x94\x83 % \xe2\x94\x83 Comment \xe2\x94\x83 % \xe2\x94\x83\n\xe2\x94\xa1\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x95\x87\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x95\x87\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x95\x87\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x95\x87\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x95\x87\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x95\x87\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xa9\n\xe2\x94\x82 JSON \xe2\x94\x82 5 \xe2\x94\x82 1.0 \xe2\x94\x82 12760 \xe2\x94\x82 76.0 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x82 PHP \xe2\x94\x82 182 \xe2\x94\x82 37.1 \xe2\x94\x82 4052 \xe2\x94\x82 43.8 \xe2\x94\x82 1288 \xe2\x94\x82 13.9 \xe2\x94\x82\n\xe2\x94\x82 JavaScript \xe2\x94\x82 37 \xe2\x94\x82 7.5 \xe2\x94\x82 3654 \xe2\x94\x82 40.4 \xe2\x94\x82 377 \xe2\x94\x82 4.2 \xe2\x94\x82\n\xe2\x94\x82 XML+PHP \xe2\x94\x82 43 \xe2\x94\x82 8.8 \xe2\x94\x82 1696 \xe2\x94\x82 89.6 \xe2\x94\x82 39 \xe2\x94\x82 2.1 \xe2\x94\x82\n\xe2\x94\x82 CSS+Lasso \xe2\x94\x82 2 \xe2\x94\x82 0.4 \xe2\x94\x82 702 \xe2\x94\x82 65.2 \xe2\x94\x82 44 \xe2\x94\x82 4.1 \xe2\x94\x82\n\xe2\x94\x82 SCSS \xe2\x94\x82 5 \xe2\x94\x82 1.0 \xe2\x94\x82 368 \xe2\x94\x82 38.2 \xe2\x94\x82 419 \xe2\x94\x82 43.5 \xe2\x94\x82\n\xe2\x94\x82 HTML+PHP \xe2\x94\x82 2 \xe2\x94\x82 0.4 \xe2\x94\x82 171 \xe2\x94\x82 85.5 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x82 Markdown \xe2\x94\x82 2 \xe2\x94\x82 0.4 \xe2\x94\x82 86 \xe2\x94\x82 55.1 \xe2\x94\x82 4 \xe2\x94\x82 2.6 \xe2\x94\x82\n\xe2\x94\x82 XML \xe2\x94\x82 1 \xe2\x94\x82 0.2 \xe2\x94\x82 29 \xe2\x94\x82 93.5 \xe2\x94\x82 2 \xe2\x94\x82 6.5 \xe2\x94\x82\n\xe2\x94\x82 Text only \xe2\x94\x82 1 \xe2\x94\x82 0.2 \xe2\x94\x82 2 \xe2\x94\x82 100.0 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x82 __unknown__ \xe2\x94\x82 132 \xe2\x94\x82 26.9 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x82 __empty__ \xe2\x94\x82 6 \xe2\x94\x82 1.2 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x82 __duplicate__ \xe2\x94\x82 6 \xe2\x94\x82 1.2 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x82 __binary__ \xe2\x94\x82 67 \xe2\x94\x82 13.6 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82 0 \xe2\x94\x82 0.0 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 Sum \xe2\x94\x82 491 \xe2\x94\x82 100.0 \xe2\x94\x82 23520 \xe2\x94\x82 59.7 \xe2\x94\x82 2173 \xe2\x94\x82 5.5 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n\n
Run Code Online (Sandbox Code Playgroud)\n结果好坏参半,最接近现实的似乎是gocloc
最快的:
我知道这个问题被标记为bash,但似乎你要解决的问题也与PHP有关.
塞巴斯蒂安·贝格曼(Sebastian Bergmann)编写了一个名为PHPLOC的工具,可以完成您想要的工作,并在此基础上为您提供项目复杂性的概述.这是其报告的一个例子:
Size
Lines of Code (LOC) 29047
Comment Lines of Code (CLOC) 14022 (48.27%)
Non-Comment Lines of Code (NCLOC) 15025 (51.73%)
Logical Lines of Code (LLOC) 3484 (11.99%)
Classes 3314 (95.12%)
Average Class Length 29
Average Method Length 4
Functions 153 (4.39%)
Average Function Length 1
Not in classes or functions 17 (0.49%)
Complexity
Cyclomatic Complexity / LLOC 0.51
Cyclomatic Complexity / Number of Methods 3.37
Run Code Online (Sandbox Code Playgroud)
如您所见,从开发人员的角度来看,所提供的信息更有用,因为它可以粗略地告诉您在开始使用项目之前项目的复杂程度.
猜测没有人会看到这个埋在后面......然而到目前为止,没有一个答案能解决带有空格的文件名问题.此外,xargs
如果树中的路径总长度超过shell环境大小限制(Linux中默认为几兆字节),则所有使用都会失败.这是一个以非常直接的方式解决这些问题的方法.子shell使用空格处理文件.在awk
总计单个文件的流wc
输出,所以应该永远用完的空间.它还exec
仅限制to文件(跳过目录):
find . -type f -name '*.php' -exec bash -c 'wc -l "$0"' {} \; | awk '{s+=$1} END {print s}'
Run Code Online (Sandbox Code Playgroud)
小智 5
首先给出最长的文件(即,也许这些长文件需要一些重构?),并排除一些供应商目录:
find . -name '*.php' | xargs wc -l | sort -nr | egrep -v "libs|tmp|tests|vendor" | less
Run Code Online (Sandbox Code Playgroud)
WC -L?更好地使用GREP -C ^
wc -l?错误! wc命令计算新行代码,而不是行!当文件中的最后一行没有以新行代码结束时,这将不计算在内!
如果您仍想要计数行,请使用 grep -c ^,完整示例:
#this example prints line count for all found files
total=0
find /path -type f -name "*.php" | while read FILE; do
#you see use grep instead wc ! for properly counting
count=$(grep -c ^ < "$FILE")
echo "$FILE has $count lines"
let total=total+count #in bash, you can convert this for another shell
done
echo TOTAL LINES COUNTED: $total
Run Code Online (Sandbox Code Playgroud)
最后,注意wc -l陷阱 (计数输入,而不是线!!!)
codel
您可以使用名为(链接)的实用程序。这是一个简单的 Python 模块,用于计算具有彩色格式的行数。
pip install codel
Run Code Online (Sandbox Code Playgroud)
要计算 C++ 文件(带扩展名)的行数.cpp
,.h
请使用:
codel count -e .cpp .h
Run Code Online (Sandbox Code Playgroud)
您还可以忽略一些 .gitignore 格式的文件/文件夹:
codel count -e .py -i tests/**
Run Code Online (Sandbox Code Playgroud)
它将忽略文件夹中的所有文件tests/
。
输出看起来像:
您还可以使用标志缩短输出-s
。它将隐藏每个文件的信息并仅显示有关每个扩展名的信息。示例如下: