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

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

wc -l *.php 

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

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

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


当前回答

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陷阱(计数进入,而不是行!!!)

其他回答

如果您使用窗口,请执行以下两个步骤:

安装cloc,例如为admin打开cmd并编写下一个代码=>choco安装cloc然后在项目文件夹中使用cd或openterminal并编写下一个代码=>cloc项目示例

步骤如下:

p.s.需要使用生成项目和node_modules移动或删除文件夹

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陷阱(计数进入,而不是行!!!)

不同之处:

wc -l `tree -if --noreport | grep -e'\.php$'`

这很好,但您需要在当前文件夹或其子文件夹中至少有一个*.php文件,否则wc会停止。

至少在OS X上,其他一些答案中列出的find+xarg+wc命令在大型列表中多次打印“总计”,但没有给出完整的总计。我能够使用以下命令获得.c文件的单个总数:

查找-名称'*.c'-print0|xargs-0wc-l|grep-v total|awk'{sum+=$1;}END{print“sum:”sum;}'

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

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