我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。
我们不需要忽视评论,因为我们只是想得到一个粗略的想法。
wc -l *.php
该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。
find . -name '*.php' | wc -l
重新输入目录中所有文件的正确语法是什么?
我们有一个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
其他回答
对于Windows,一个简单快捷的工具是LocMetrics。
使用find的-exec和awk。我们来了:
find . -type f -exec wc -l {} \; | awk '{ SUM += $0} END { print SUM }'
此代码段查找所有文件(-type f)。要按文件扩展名查找,请使用-name:
find . -name '*.py' -exec wc -l '{}' \; | awk '{ SUM += $0; } END { print SUM; }'
我想检查多个文件类型,并懒得手动计算总数。所以我现在用这个来一次性得到总数。
find . -name '*.js' -or -name '*.php' | xargs wc -l | grep 'total' | awk '{ SUM += $1; print $1} END { print "Total text lines in PHP and JS",SUM }'
7935115318PHP和JS中的文本行总数94669
这允许您链接多个想要过滤的扩展类型。只需将它们添加到-name“*.js”-或-name“*.php”部分,并可能根据您的喜好修改otuput消息。
Try:
find . -name '*.php' | xargs wc -l
或(当文件名包含特殊字符(如空格)时)
find . -name '*.php' | sed 's/.*/"&"/' | xargs wc -l
SLOCCount工具也可能有帮助。
它将提供准确的源代码行计数你指向的层次结构,以及一些额外的统计数据。
排序输出:
查找-名称“*.php”| xargs wc-l | sort-nr
我知道这个问题被标记为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
正如您所看到的,从开发人员的角度来看,所提供的信息非常有用,因为它可以在您开始使用项目之前大致告诉您项目有多复杂。