我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。
我们不需要忽视评论,因为我们只是想得到一个粗略的想法。
wc -l *.php
该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。
find . -name '*.php' | wc -l
重新输入目录中所有文件的正确语法是什么?
我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。
我们不需要忽视评论,因为我们只是想得到一个粗略的想法。
wc -l *.php
该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。
find . -name '*.php' | wc -l
重新输入目录中所有文件的正确语法是什么?
当前回答
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 {} +
每个文件中的行,按文件路径排序
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
每个文件中的行,按行数降序排序
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
所有文件中的总行数
find . -name '*.php' -type f -exec cat {} + | wc -l
其他回答
bash工具总是很好用的,但是为了这个目的,只使用一个这样做的工具似乎更有效率。截至2022年,我玩了一些主要游戏,即cloc(perl)、gocloc(go)、pygount(python)。
在没有太多调整的情况下获得了各种结果。看起来最准确、最快的是gocloc。
带有vue前端的小型laravel项目示例:
高氯酸盐
$ ~/go/bin/gocloc /home/jmeyo/project/sequasa
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JSON 5 0 0 16800
Vue 96 1181 137 8993
JavaScript 37 999 454 7604
PHP 228 1493 2622 7290
CSS 2 157 44 877
Sass 5 72 426 466
XML 11 0 2 362
Markdown 2 45 0 111
YAML 1 0 0 13
Plain Text 1 0 0 2
-------------------------------------------------------------------------------
TOTAL 388 3947 3685 42518
-------------------------------------------------------------------------------
cloc
$ cloc /home/jmeyo/project/sequasa
450 text files.
433 unique files.
40 files ignored.
github.com/AlDanial/cloc v 1.90 T=0.24 s (1709.7 files/s, 211837.9 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JSON 5 0 0 16800
Vuejs Component 95 1181 370 8760
JavaScript 37 999 371 7687
PHP 180 1313 2600 5321
Blade 48 180 187 1804
SVG 27 0 0 1273
CSS 2 157 44 877
XML 12 0 2 522
Sass 5 72 418 474
Markdown 2 45 0 111
YAML 4 11 37 53
-------------------------------------------------------------------------------
SUM: 417 3958 4029 43682
-------------------------------------------------------------------------------
平山
$ pygount --format=summary /home/jmeyo/project/sequasa
┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━┓
┃ Language ┃ Files ┃ % ┃ Code ┃ % ┃ Comment ┃ % ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━┩
│ JSON │ 5 │ 1.0 │ 12760 │ 76.0 │ 0 │ 0.0 │
│ PHP │ 182 │ 37.1 │ 4052 │ 43.8 │ 1288 │ 13.9 │
│ JavaScript │ 37 │ 7.5 │ 3654 │ 40.4 │ 377 │ 4.2 │
│ XML+PHP │ 43 │ 8.8 │ 1696 │ 89.6 │ 39 │ 2.1 │
│ CSS+Lasso │ 2 │ 0.4 │ 702 │ 65.2 │ 44 │ 4.1 │
│ SCSS │ 5 │ 1.0 │ 368 │ 38.2 │ 419 │ 43.5 │
│ HTML+PHP │ 2 │ 0.4 │ 171 │ 85.5 │ 0 │ 0.0 │
│ Markdown │ 2 │ 0.4 │ 86 │ 55.1 │ 4 │ 2.6 │
│ XML │ 1 │ 0.2 │ 29 │ 93.5 │ 2 │ 6.5 │
│ Text only │ 1 │ 0.2 │ 2 │ 100.0 │ 0 │ 0.0 │
│ __unknown__ │ 132 │ 26.9 │ 0 │ 0.0 │ 0 │ 0.0 │
│ __empty__ │ 6 │ 1.2 │ 0 │ 0.0 │ 0 │ 0.0 │
│ __duplicate__ │ 6 │ 1.2 │ 0 │ 0.0 │ 0 │ 0.0 │
│ __binary__ │ 67 │ 13.6 │ 0 │ 0.0 │ 0 │ 0.0 │
├───────────────┼───────┼───────┼───────┼───────┼─────────┼──────┤
│ Sum │ 491 │ 100.0 │ 23520 │ 59.7 │ 2173 │ 5.5 │
└───────────────┴───────┴───────┴───────┴───────┴─────────┴──────┘
结果喜忧参半,最接近现实的似乎是gocloc,也是迄今为止最快的:
cloc:0m0.430s高氯酸盐:0m0.059spygcount:0m39.980秒
您需要一个简单的for循环:
total_count=0
for file in $(find . -name *.php -print)
do
count=$(wc -l $file)
let total_count+=count
done
echo "$total_count"
您可以使用此windows power shell代码来计算所需的任何文件类型:
(gci -include *.cs,*.cshtml -recurse | select-string .).Count
至少在OS X上,其他一些答案中列出的find+xarg+wc命令在大型列表中多次打印“总计”,但没有给出完整的总计。我能够使用以下命令获得.c文件的单个总数:
查找-名称'*.c'-print0|xargs-0wc-l|grep-v total|awk'{sum+=$1;}END{print“sum:”sum;}'
您不需要所有这些复杂且难以记忆的命令。您只需要一个名为行计数器的Python工具。
快速概述
这就是你获得工具的方法
$ pip install line-counter
使用line命令获取当前目录下的文件计数和行计数(递归):
$ line
Search in /Users/Morgan/Documents/Example/
file count: 4
line count: 839
如果你想要更多的细节,只需使用行-d。
$ line -d
Search in /Users/Morgan/Documents/Example/
Dir A/file C.c 72
Dir A/file D.py 268
file A.py 467
file B.c 32
file count: 4
line count: 839
这个工具最好的部分是,你可以在其中添加一个.gitignore类配置文件。你可以设置规则来选择或忽略要计数的文件类型,就像你在.gitignore'中所做的那样。
更多描述和用法如下:https://github.com/MorganZhang100/line-counter