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

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

wc -l *.php 

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

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

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


当前回答

在类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
-------------------------------------------------------------------------------

其他回答

您可以使用专门为此目的而构建的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
-------------------------------------------------------------------------------

如果您想保持简单,请去掉中间人,只需使用所有文件名调用wc:

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

或者用现代语法:

wc -l $(find . -name "*.php")

只要任何目录名或文件名中没有空格,这就可以工作。只要您没有数以万计的文件(现代shell支持非常长的命令行)。您的项目有74个文件,因此您有足够的空间进行扩展。

不包括空行:

find . -name "*.php" | xargs grep -v -c '^$' | awk 'BEGIN {FS=":"} { $cnt = $cnt + $2} END {print $cnt}'

包括空行:

find . -name "*.php" | xargs wc -l

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

仅适用于来源:

wc `find`

要过滤,只需使用grep:

wc `find | grep .php$`