Ho1*_*Ho1 63
最简单的方法是使用名为 的工具cloc
。以这种方式使用它:
cloc .
Run Code Online (Sandbox Code Playgroud)
就是这样。:-)
Ste*_*itt 44
你或许应该使用SLOCCount或CLOC为此,他们正在为一个项目计算行源代码而设计的,不管目录结构等; 任何一个
sloccount .
Run Code Online (Sandbox Code Playgroud)
或者
cloc .
Run Code Online (Sandbox Code Playgroud)
将生成关于从当前目录开始的所有源代码的报告。
如果你想使用find
and wc
,GNUwc
有一个不错的--files0-from
选择:
find . -name '*.[ch]' -print0 | wc --files0-from=- -l
Run Code Online (Sandbox Code Playgroud)
hee*_*ayl 12
由于该wc
命令可以接受多个参数,因此您可以将所有文件名传递给wc
using GNU 操作的+
参数:-exec
find
find . -type f -name '*.[ch]' -exec wc -l {} +
Run Code Online (Sandbox Code Playgroud)
或者,在 中bash
,使用 shell 选项globstar
递归遍历目录:
shopt -s globstar
wc -l **/*.[ch]
Run Code Online (Sandbox Code Playgroud)
其他外壳默认情况下递归遍历(例如zsh
)或具有类似的选项,例如globstar
,至少大多数。
您可以find
与xargs
和一起使用wc
:
find . -type f -name '*.h' -o -name '*.c' | xargs wc -l
Run Code Online (Sandbox Code Playgroud)
如果您处于无法访问cloc
等的环境中,我建议您
find -name '*.[ch]' -type f -exec cat '{}' + | grep -c '[^[:space:]]'
Run Code Online (Sandbox Code Playgroud)
Run-through:find
递归搜索所有名称以.c
or结尾的常规文件,.h
并cat
在其上运行。输出通过管道传输grep
以计算所有非空行(包含至少一个非空格字符的行)。