Linux 在所有子目录上使用 wc 来计算行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13727917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use wc on all subdirectories to count the sum of lines
提问by Jonas Stein
How can I count all lines of all files in all subdirectories with wc
?
如何计算所有子目录中所有文件的所有行wc
?
cd mydir
wc -l *
..
11723 total
man wc
suggests wc -l --files0-from=-
, but I do not know how to generate the list of all files as NUL-terminated names
man wc
建议wc -l --files0-from=-
,但我不知道如何生成所有文件的列表NUL-terminated names
find . -print | wc -l --files0-from=-
did not work.
不工作。
采纳答案by gniourf_gniourf
You probably want this:
你可能想要这个:
find . -type f -print0 | wc -l --files0-from=-
If you only want the total number of lines, you could use
如果你只想要总行数,你可以使用
find . -type f -exec cat {} + | wc -l
回答by sge
I would suggest something like
我会建议像
find ./ -type f | xargs wc -l | cut -c 1-8 | awk '{total += } END {print total}'
回答by ДМИТРИЙ МАЛИКОВ
Perhaps you are looking for exec
option of find
.
也许您正在寻找 的exec
选项find
。
find . -type f -exec wc -l {} \; | awk '{total += } END {print total}'
回答by Cronk
Bit late to the game here, but wouldn't this also work? find . -type f | wc -l
这里的游戏有点晚了,但这也不行吗? find . -type f | wc -l
This counts all lines output by the 'find' command. You can fine-tune the 'find' to show whatever you want. I am using it to count the number of subdirectories, in one specific subdir, in deep tree: find ./*/*/*/*/*/*/TOC -type d | wc -l
. Output: 76435
. (Just doing a find without all the intervening asterisks yielded an error.)
这会计算“find”命令输出的所有行。您可以微调“查找”以显示您想要的任何内容。我正在使用它来计算深树中一个特定子目录中的子目录数: find ./*/*/*/*/*/*/TOC -type d | wc -l
. 输出:76435
。(只是在没有所有中间星号的情况下进行查找会产生错误。)
回答by Ankur
To count all lines for specific file extension u can use ,
要计算您可以使用的特定文件扩展名的所有行,
find . -name '*.fileextension' | xargs wc -l
if you want it on two or more different types of files u can put -o option
如果你想在两个或更多不同类型的文件上使用它,你可以放 -o 选项
find . -name '*.fileextension1' -o -name '*.fileextension2' | xargs wc -l
回答by terdon
Another option would be to use a recursive grep:
另一种选择是使用递归 grep:
grep -hRc '' . | awk '{k+=}END{print k}'
The awk simply adds the numbers. The grep
options used are:
awk 只是将数字相加。使用的grep
选项是:
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
-h, --no-filename
Suppress the prefixing of file names on output. This is the
default when there is only one file (or only standard input) to
search.
-R, --dereference-recursive
Read all files under each directory, recursively. Follow all
symbolic links, unlike -r.
The grep
, therefore, counts the number of lines matching anything (''), so essentially just counts the lines.
的grep
,因此,计算匹配任何东西(“”)的行数,所以本质上只是计数线。
回答by Wolfgang Fahl
Based on ДМИТРИЙ МАЛИКОВ's answer:
基于 ДМИТРИЙ МАЛИКОВ 的回答:
Example for counting lines of java code with formatting:
使用格式计算 Java 代码行数的示例:
one liner
一个班轮
find . -name *.java -exec wc -l {} \; | awk '{printf ("%3d: %6d %s\n",NR,,); total += } END {printf (" %6d\n",total)}'
awk part:
awk 部分:
{
printf ("%3d: %6d %s\n",NR,,);
total +=
}
END {
printf (" %6d\n",total)
}
example result
示例结果
1: 120 ./opencv/NativeLibrary.java
2: 65 ./opencv/OsCheck.java
3: 5 ./opencv/package-info.java
190