bash - linux统计文件行数 - wc文件夹
如何递归计算目录中的所有代码行? (20)
我们有一个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 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陷阱(计数输入,而不是行数!!!)
POSIX
每个文件中的行:
find . -name '*.php' -type f | xargs wc -l
每个文件中的行,按文件路径排序
find . -name '*.php' -type f | sort | xargs wc -l
每个文件中的行按行数排序,递减
find . -name '*.php' -type f | xargs wc -l | sort -nr
所有文件中的总行数
find . -name '*.php' -type f | xargs cat | wc -l
一个简单的快速的将会使用find
所有搜索/过滤功能,当文件太多(数字参数溢出)时,不会失败,对于名称中带有滑稽符号的文件可以正常工作,不使用xargs
,不会启动无用的高数量的外部命令(感谢find
for的-exec
)。 干得好:
find . -name '*.php' -type f -exec cat -- {} + | wc -l
仅用于来源:
wc `find`
要过滤,只需使用grep
wc `find | grep .php$`
你也可以尝试cloc.sourceforge.net (需要Perl)
你想要的是一个简单的for循环:
total_count=0
for file in $(find . -name *.php -print)
do
count=$(wc -l $file)
let total_count+=count
done
echo $total_count
另一个命令是获得所有文件的总和(当然是Linux)
find ./ -type f -exec wc -l {} \; | cut -d' ' -f1 | paste -sd+ | bc
与其他答案的主要区别:
- 使用find -exec ,
- 使用粘贴(与切割) ,
- 使用bc
在类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
-------------------------------------------------------------------------------
如果使用Bash(或ZSH)的新版本,它更简单:
wc -l **/*.php
在Bash shell中,这需要设置globstar
选项,否则**
glob-operator不是递归的。 要启用此设置,请发出
shopt -s globstar
为了使其成为永久的,将它添加到其中一个初始化文件( ~/.bashrc
, ~/.bash_profile
等)。
如果您只需要总数的行数,让我们说您的PHP文件,即使在Windows下,如果您安装了GnuWin32,也可以使用非常简单的一行命令。 喜欢这个:
cat `/gnuwin32/bin/find.exe . -name *.php` | wc -l
您需要指定find.exe的确切位置,否则将执行Windows提供的FIND.EXE(来自旧DOS类命令),因为它可能位于环境PATH中的GnuWin32之前,并且具有不同的参数和结果。
请注意,在上面的命令中,你应该使用反引号,而不是单引号。
对于另一个单线:
( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l
使用空格名称,只输出一个数字。
对于我来说更普遍和简单,假设你需要计算不同名称扩展名的文件(比如说也是本地文件)
wc `find . -name '*.[h|c|cpp|php|cc]'`
您没有指定有多少个文件或什么是所需的输出。 这是你想要的:
find . -name '*.php' | xargs wc -l
我使用了从src-project目录启动的这个内联脚本:
for i in $(find . -type f); do rowline=$(wc -l $i | cut -f1 -d" "); file=$(wc -l $i | cut -f2 -d" "); lines=$((lines + rowline)); echo "Lines["$lines"] " $file "has "$rowline"rows."; done && unset lines
这产生了这个输出:
Lines[75] ./Db.h has 75rows.
Lines[143] ./Db.cpp has 68rows.
Lines[170] ./main.cpp has 27rows.
Lines[294] ./Sqlite.cpp has 124rows.
Lines[349] ./Sqlite.h has 55rows.
Lines[445] ./Table.cpp has 96rows.
Lines[480] ./DbError.cpp has 35rows.
Lines[521] ./DbError.h has 41rows.
Lines[627] ./QueryResult.cpp has 106rows.
Lines[717] ./QueryResult.h has 90rows.
Lines[828] ./Table.h has 111rows.
我知道这个问题被标记为bash ,但似乎你想解决的问题也是PHP相关的。
塞巴斯蒂安贝格曼写了一个名为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
正如您所看到的,从开发人员的角度来看,所提供的信息更加有用,因为它可以粗略地告诉您在开始使用它之前项目有多复杂。
有一个名为sloccount的小工具来计算目录中的代码行。 应该注意的是,它比你想要的要多,因为它忽略空行/注释,按照编程语言对结果进行分组,并计算一些统计数据。
猜测没有人会看到它埋在后面......但迄今没有任何答案可以解决带空格的文件名问题。 另外,如果树中路径的总长度超过shell环境大小限制(在Linux中默认为几兆字节),那么所有使用xargs
的应用程序都会失败。 这是一个以相当直接的方式解决这些问题的方法。 subshell使用空格来处理文件。 awk
总结了单个文件wc
输出的流,所以不应该耗尽空间。 它还将exec
限制为仅文件(跳过目录):
find . -type f -name '*.php' -exec bash -c 'wc -l "$0"' {} \; | awk '{s+=$1} END {print s}'
至少在OS X上,某些其他答案中列出的find + xarg + wc命令会在大型列表中多次打印“总计”,并且没有给出完整的总计。 我可以使用以下命令获得一个.c文件的总数:
find . -name '*.c' -print0 |xargs -0 wc -l|grep -v total|awk '{ sum += $1; } END { print "SUM: " sum; }'
非常简单
find /path -type f -name "*.php" | while read FILE
do
count=$(wc -l < $FILE)
echo "$FILE has $count lines"
done
首先给出最长的文件(也许这些长文件需要一些重构爱),并排除一些供应商目录:
find . -name '*.php' | xargs wc -l | sort -nr | egrep -v "libs|tmp|tests|vendor" | less