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

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

wc -l *.php 

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

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

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


当前回答

如果使用相当新的Bash(或ZSH)版本,则要简单得多:

wc -l **/*.php

在Bash shell中,这需要设置globstar选项,否则**glob运算符不是递归的。要启用此设置,请发出

shopt -s globstar

要使其永久化,请将其添加到一个初始化文件(~/.bashrc、~/.bah_profile等)中。

其他回答

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 of '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

最后,注意wc-l陷阱(计数进入,而不是行!!!)

您没有指定有多少个文件或所需的输出。

这可能是您想要的:

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

Try:

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

或(当文件名包含特殊字符(如空格)时)

find . -name '*.php' | sed 's/.*/"&"/' | xargs  wc -l

SLOCCount工具也可能有帮助。

它将提供准确的源代码行计数你指向的层次结构,以及一些额外的统计数据。

排序输出:

查找-名称“*.php”| xargs wc-l | sort-nr

我的Windows系统上安装了BusyBox。这就是我所做的。

ECHO OFF
for /r %%G in (*.php) do (
busybox grep . "%%G" | busybox wc -l
)

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

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