我们有一个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

其他回答

有一个叫做sloccount的小工具来计算目录中的代码行。

应该注意的是,它做的比你想要的更多,因为它忽略空行/注释,按编程语言分组结果,并计算一些统计数据。

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秒

到目前为止,没有一个答案能解决带空格的文件名问题。

此外,如果树中路径的总长度超过shell环境大小限制(在Linux中默认为几兆字节),则所有使用xargs的路径都会失败。

这是一个以非常直接的方式解决这些问题的方法。子shell处理带有空格的文件。awk汇总了单个文件wc输出的流,因此它不应该耗尽空间。它还将exec限制为仅文件(跳过目录):

find . -type f -name '*.php' -exec bash -c 'wc -l "$0"' {} \; | awk '{s+=$1} END {print s}'
cat \`find . -name "*.php"\` | wc -l
$cd directory
$wc -l* | sort -nr