我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。

我们不需要忽视评论,因为我们只是想得到一个粗略的想法。

wc -l *.php 

该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。

find . -name '*.php' | wc -l

重新输入目录中所有文件的正确语法是什么?


当前回答

在Windows PowerShell上尝试以下操作:

dir -Recurse *.php | Get-Content | Measure-Object -Line

其他回答

虽然我喜欢这些脚本,但我更喜欢这个脚本,因为它还显示了每个文件的摘要,只要总计:

wc -l `find . -name "*.php"`

我知道这个问题被标记为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

正如您所看到的,从开发人员的角度来看,所提供的信息非常有用,因为它可以在您开始使用项目之前大致告诉您项目有多复杂。

您不需要所有这些复杂且难以记忆的命令。您只需要一个名为行计数器的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

对于另一个衬垫:

( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l

它处理带有空格的名称,只输出一个数字。

如果文件太多,最好只查找总行数。

find . -name '*.php' | xargs wc -l | grep -i ' total' | awk '{print $1}'