[英]How to count all the lines of code in a directory recursively using or operator to include several file types
[英]How can I count all the lines of code in a directory recursively?
我们有一个 PHP 应用程序,想要统计特定目录及其子目录下的所有代码行数。
我们不需要忽略评论,因为我们只是想得到一个粗略的想法。
wc -l *.php
该命令适用于给定目录,但它会忽略子目录。 我在想下面的评论可能有用,但它返回 74,这绝对不是这样的......
find . -name '*.php' | wc -l
从一个目录中重新输入所有文件的正确语法是什么?
尝试:
find . -name '*.php' | xargs wc -l
或(当文件名包含特殊字符如空格时)
find . -name '*.php' | sed 's/.*/"&"/' | xargs wc -l
SLOCCount 工具也可能有所帮助。
它将为您指向的任何层次结构提供准确的源代码行数,以及一些额外的统计信息。
排序输出:
find . -name '*.php' | xargs wc -l | sort -nr
对于另一个单线:
( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l
它适用于带有空格的名称,并且只输出一个数字。
如果使用最新版本的 Bash(或 ZSH),它会简单得多:
wc -l **/*.php
在 Bash shell 中,这需要设置globstar
选项,否则**
glob-operator 不是递归的。 要启用此设置,请发出
shopt -s globstar
要使其永久化,请将其添加到初始化文件之一( ~/.bashrc
、 ~/.bash_profile
等)。
您可以使用专为此目的而构建的cloc
实用程序。 它报告每种语言的行数,以及其中有多少是注释等。CLOC 可在 Linux、Mac 和 Windows 上使用。
用法和输出示例:
$ cloc --exclude-lang=DTD,Lua,make,Python .
2570 text files.
2200 unique files.
8654 files ignored.
http://cloc.sourceforge.net v 1.53 T=8.0 s (202.4 files/s, 99198.6 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JavaScript 1506 77848 212000 366495
CSS 56 9671 20147 87695
HTML 51 1409 151 7480
XML 6 3088 1383 6222
-------------------------------------------------------------------------------
SUM: 1619 92016 233681 467892
-------------------------------------------------------------------------------
在类 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
-------------------------------------------------------------------------------
您没有指定有多少文件或所需的输出是什么。
这可能是您正在寻找的:
find . -name '*.php' | xargs wc -l
另一个变化:)
$ find . -name '*.php' | xargs cat | wc -l
这将给出总和,而不是逐个文件。
添加.
find
后让它工作。
使用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; }'
对我来说更常见和简单,假设您需要计算不同名称扩展名的文件(例如,也是本地人):
wc $(find . -type f | egrep "\.(h|c|cpp|php|cc)" )
有一个叫做sloccount的小工具可以计算目录中的代码行数。
应该注意的是,它比您想要的更多,因为它忽略空行/注释,按编程语言对结果进行分组并计算一些统计信息。
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
你想要一个简单的for
循环:
total_count=0
for file in $(find . -name *.php -print)
do
count=$(wc -l $file)
let total_count+=count
done
echo "$total_count"
Tokei工具显示有关目录中代码的统计信息。 Tokei 将显示文件数、这些文件中的总行数以及按语言分组的代码、注释和空白。 Tokei 也可在 Mac、Linux 和 Windows 上使用。
Tokei 的输出示例如下:
$ tokei
-------------------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-------------------------------------------------------------------------------
CSS 2 12 12 0 0
JavaScript 1 435 404 0 31
JSON 3 178 178 0 0
Markdown 1 9 9 0 0
Rust 10 408 259 84 65
TOML 3 69 41 17 11
YAML 1 30 25 0 5
-------------------------------------------------------------------------------
Total 21 1141 928 101 112
-------------------------------------------------------------------------------
可以按照存储库中 README 文件上的说明安装 Tokei。
一个简单的,速度很快,将使用find
所有搜索/过滤功能,当文件太多时不会失败(数字参数溢出),可以很好地处理名称中带有有趣符号的文件,而不使用xargs
,并且不要启动大量无用的外部命令(感谢+
for find
的-exec
)。 干得好:
find . -name '*.php' -type f -exec cat -- {} + | wc -l
我知道这个问题被标记为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
如您所见,从开发人员的角度来看,提供的信息要有用得多,因为它可以在您开始使用项目之前粗略地告诉您项目的复杂程度。
到目前为止,没有一个答案涉及带空格的文件名问题。
此外,如果树中路径的总长度超过 shell 环境大小限制(在 Linux 中默认为几兆字节),则所有使用xargs
的都会失败。
这是一种以非常直接的方式解决这些问题的方法。 子shell 负责处理带有空格的文件。 awk
总计单个文件wc
输出的流,因此它永远不会耗尽空间。 它还将exec
限制为仅文件(跳过目录):
find . -type f -name '*.php' -exec bash -c 'wc -l "$0"' {} \; | awk '{s+=$1} END {print s}'
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
陷阱(计数进入,而不是行数!!!)
对于Windows ,一个简单快捷的工具是LocMetrics 。
很简单:
find /path -type f -name "*.php" | while read FILE
do
count=$(wc -l < $FILE)
echo "$FILE has $count lines"
done
如果您希望按行数对结果进行排序,只需添加| sort
| sort
或| sort -r
| sort -r
( -r
表示降序)到第一个答案,如下所示:
find . -name '*.php' | xargs wc -l | sort -r
首先给出最长的文件(即,也许这些长文件需要一些重构的爱?),并排除一些供应商目录:
find . -name '*.php' | xargs wc -l | sort -nr | egrep -v "libs|tmp|tests|vendor" | less
如果你想保持简单,去掉中间人,只用所有文件名调用wc
:
wc -l `find . -name "*.php"`
或者在现代语法中:
wc -l $(find . -name "*.php")
只要任何目录名或文件名中没有空格,这就会起作用。 只要您没有数以万计的文件(现代 shell 支持非常长的命令行)。 您的项目有 74 个文件,因此您有足够的发展空间。
您可以使用名为codel
( link ) 的实用程序。 这是一个简单的 Python 模块,用于计算具有彩色格式的行。
pip install codel
要计算 C++ 文件的行数(带有.cpp
和.h
扩展名),请使用:
codel count -e .cpp .h
您还可以忽略一些 .gitignore 格式的文件/文件夹:
codel count -e .py -i tests/**
它将忽略tests/
文件夹中的所有文件。
输出看起来像:
您还可以使用-s
标志缩短输出。 它将隐藏每个文件的信息,只显示每个扩展名的信息。 示例如下:
虽然我喜欢这些脚本,但我更喜欢这个脚本,因为它还显示了每个文件的摘要,只要总数:
wc -l `find . -name "*.php"`
您不需要所有这些复杂且难以记住的命令。 您只需要一个名为line-counter的Python工具。
快速概览
这就是你获得工具的方式
$ pip install line-counter
使用line
命令获取当前目录下的文件数和行数(递归):
$ line
Search in /Users/Morgan/Documents/Example/
file count: 4
line count: 839
如果您想要更多详细信息,只需使用line -d
。
$ line -d
Search in /Users/Morgan/Documents/Example/
Dir A/file C.c 72
Dir A/file D.py 268
file A.py 467
file B.c 32
file count: 4
line count: 839
这个工具最好的部分是,你可以向它添加一个类似.gitignore
的配置文件。 您可以设置规则来选择或忽略要计算的文件类型,就像您在“.gitignore”中所做的一样。
更多描述和用法在这里: https : //github.com/MorganZhang100/line-counter
我们有一个PHP应用程序,并希望计算特定目录及其子目录下的所有代码行。
我们不需要忽略评论,因为我们只是想获得一个大概的想法。
wc -l *.php
该命令对于给定目录非常有效,但是会忽略子目录。 我以为以下评论可能有用,但返回74,这绝对不是这种情况...
find . -name '*.php' | wc -l
递归地从目录中馈送所有文件的正确语法是什么?
如果你只需要总行数,比如说你的 PHP 文件,如果你安装了 GnuWin32,你甚至可以在 Windows 下使用非常简单的一行命令。 像这样:
cat `/gnuwin32/bin/find.exe . -name *.php` | wc -l
您需要指定 find.exe 的确切位置,否则将执行 Windows 提供的FIND.EXE (来自旧的类似 DOS 的命令),因为它可能在环境 PATH 中的 GnuWin32 之前并且具有不同的参数和结果。
请注意,在上面的命令中,您应该使用反引号,而不是单引号。
如果文件太多,最好只查找总行数。
find . -name '*.php' | xargs wc -l | grep -i ' total' | awk '{print $1}'
类似于Shizzmo 的答案,但更丑更准确。 如果您经常使用它,请对其进行修改以适应并将其放入脚本中。
这个例子:
find
遍历)find . \! \( \( -path ./lib -o -path ./node_modules -o -path ./vendor -o -path ./any/other/path/to/skip -o -wholename ./not/this/specific/file.php -o -name '*.min.js' -o -name '*.min.css' \) -prune \) -type f \( -name '*.php' -o -name '*.inc' -o -name '*.js' -o -name '*.scss' -o -name '*.css' \) -print0 | xargs -0 cat | grep -vcE '^[[:space:]]*$'
$cd directory
$wc -l* | sort -nr
至少在OS X上,其他一些答案中列出的find + xarg + wc命令在大型列表上多次打印“total”,并且没有给出完整的总数。 我能够使用以下命令获得.c文件的总数:
find . -name '*.c' -print0 |xargs -0 wc -l|grep -v total|awk '{ sum += $1; } END { print "SUM: " sum; }'
我使用了这个从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.
排除空白行
find . -name "*.php" | xargs grep -v -c '^$' | awk 'BEGIN {FS=":"} { $cnt = $cnt + $2} END {print $cnt}'
包括空行:
find . -name "*.php" | xargs wc -l
我想检查多种文件类型,懒得手动计算总数。 所以我现在用它来得到一个 go 的总数。
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 }'
79351
15318
PHP 和 JS 94669 中的总文本行
这允许您链接您希望过滤的多个扩展类型。 只需将它们添加到-name '*.js' -or -name '*.php'
部分,并可能根据自己的喜好修改 otuput 消息。
首先更改您想知道行数的目录。
例如,如果我想知道名为sample的目录的所有文件中的行数,请给出$cd sample
。
然后尝试命令$wc -l *
。 这将返回每个文件的行数以及最后整个目录中的总行数。
我这样做:
下面是lineCount.c文件实现:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getLinesFromFile(const char*);
int main(int argc, char* argv[]) {
int total_lines = 0;
for(int i = 1; i < argc; ++i) {
total_lines += getLinesFromFile(argv[i]); // *argv is a char*
}
printf("You have a total of %d lines in all your file(s)\n", total_lines);
return 0;
}
int getLinesFromFile(const char* file_name) {
int lines = 0;
FILE* file;
file = fopen(file_name, "r");
char c = ' ';
while((c = getc(file)) != EOF)
if(c == '\n')
++lines;
fclose(file);
return lines;
}
现在打开命令行并输入gcc lineCount.c
。 然后输入./a.out *.txt
。
这将显示目录中以 .txt 结尾的文件的总行数。
如果你想计算你写的 LOC,你可能需要排除一些文件。
对于Django项目,您可能希望忽略migrations
和static
文件夹。 对于 JavaScript 项目,您可以排除所有图片或所有 fonts。
find . \( -path '*/migrations' -o -path '*/.git' -o -path '*/.vscode' -o -path '*/fonts' -o -path '*.png' -o -path '*.jpg' -o -path '*/.github' -o -path '*/static' \) -prune -o -type f -exec cat {} + | wc -l
这里的用法如下:
*/文件夹名称
*/。文件扩展名
要列出文件,请修改命令的后半部分:
find . \( -path '*/migrations' -o -path '*/.git' -o -path '*/.vscode' -o -path '*/fonts' -o -path '*.png' -o -path '*.jpg' -o -path '*/.github' -o -path '*/static' \) -prune -o --print
在 Windows PowerShell 上试试这个:
dir -Recurse *.php | Get-Content | Measure-Object -Line
我的 Windows 系统上安装了BusyBox 。 所以这就是我所做的。
ECHO OFF
for /r %%G in (*.php) do (
busybox grep . "%%G" | busybox wc -l
)
另一个获取所有文件总和的命令(当然是 Linux)
find ./ -type f -exec wc -l {} \; | cut -d' ' -f1 | paste -sd+ | bc
与其他答案的主要区别:
这是一个灵活的使用旧的 Python (至少在 Python 2.6 中工作)结合Shizzmo 的可爱 one-liner 。 只需使用要在源文件夹中计数的文件类型填写types
列表,然后让它飞起来:
#!/usr/bin/python
import subprocess
rcmd = "( find ./ -name '*.%s' -print0 | xargs -0 cat ) | wc -l"
types = ['c','cpp','h','txt']
sum = 0
for el in types:
cmd = rcmd % (el)
p = subprocess.Popen([cmd],stdout=subprocess.PIPE,shell=True)
out = p.stdout.read().strip()
print "*.%s: %s" % (el,out)
sum += int(out)
print "sum: %d" % (sum)
bash
工具总是很好用,但为了这个目的,只使用能做到这一点的工具似乎更有效。 截至 2022 年,我使用了一些主要的,即 cloc (perl) 、 gocloc (go) 、 pygount (python) 。
无需过多调整即可获得各种结果。 似乎最准确、速度最快的是gocloc
。
带有 vue 前端的小型 laravel 项目示例:
$ ~/go/bin/gocloc /home/jmeyo/project/sequasa
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JSON 5 0 0 16800
Vue 96 1181 137 8993
JavaScript 37 999 454 7604
PHP 228 1493 2622 7290
CSS 2 157 44 877
Sass 5 72 426 466
XML 11 0 2 362
Markdown 2 45 0 111
YAML 1 0 0 13
Plain Text 1 0 0 2
-------------------------------------------------------------------------------
TOTAL 388 3947 3685 42518
-------------------------------------------------------------------------------
$ cloc /home/jmeyo/project/sequasa
450 text files.
433 unique files.
40 files ignored.
github.com/AlDanial/cloc v 1.90 T=0.24 s (1709.7 files/s, 211837.9 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JSON 5 0 0 16800
Vuejs Component 95 1181 370 8760
JavaScript 37 999 371 7687
PHP 180 1313 2600 5321
Blade 48 180 187 1804
SVG 27 0 0 1273
CSS 2 157 44 877
XML 12 0 2 522
Sass 5 72 418 474
Markdown 2 45 0 111
YAML 4 11 37 53
-------------------------------------------------------------------------------
SUM: 417 3958 4029 43682
-------------------------------------------------------------------------------
$ pygount --format=summary /home/jmeyo/project/sequasa
┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━┓
┃ Language ┃ Files ┃ % ┃ Code ┃ % ┃ Comment ┃ % ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━┩
│ JSON │ 5 │ 1.0 │ 12760 │ 76.0 │ 0 │ 0.0 │
│ PHP │ 182 │ 37.1 │ 4052 │ 43.8 │ 1288 │ 13.9 │
│ JavaScript │ 37 │ 7.5 │ 3654 │ 40.4 │ 377 │ 4.2 │
│ XML+PHP │ 43 │ 8.8 │ 1696 │ 89.6 │ 39 │ 2.1 │
│ CSS+Lasso │ 2 │ 0.4 │ 702 │ 65.2 │ 44 │ 4.1 │
│ SCSS │ 5 │ 1.0 │ 368 │ 38.2 │ 419 │ 43.5 │
│ HTML+PHP │ 2 │ 0.4 │ 171 │ 85.5 │ 0 │ 0.0 │
│ Markdown │ 2 │ 0.4 │ 86 │ 55.1 │ 4 │ 2.6 │
│ XML │ 1 │ 0.2 │ 29 │ 93.5 │ 2 │ 6.5 │
│ Text only │ 1 │ 0.2 │ 2 │ 100.0 │ 0 │ 0.0 │
│ __unknown__ │ 132 │ 26.9 │ 0 │ 0.0 │ 0 │ 0.0 │
│ __empty__ │ 6 │ 1.2 │ 0 │ 0.0 │ 0 │ 0.0 │
│ __duplicate__ │ 6 │ 1.2 │ 0 │ 0.0 │ 0 │ 0.0 │
│ __binary__ │ 67 │ 13.6 │ 0 │ 0.0 │ 0 │ 0.0 │
├───────────────┼───────┼───────┼───────┼───────┼─────────┼──────┤
│ Sum │ 491 │ 100.0 │ 23520 │ 59.7 │ 2173 │ 5.5 │
└───────────────┴───────┴───────┴───────┴───────┴─────────┴──────┘
结果好坏参半,最接近现实的似乎是gocloc
一个,也是迄今为止最快的:
lines=0 ; for file in *.cpp *.h ; do lines=$(( $lines + $( wc -l $file | cut -d ' ' -f 1 ) )) ; done ; echo $lines
您可以使用此 windows 电源 shell 代码来计算您想要的任何文件类型:
(gci -include *.cs,*.cshtml -recurse | select-string .).Count
cat \`find . -name "*.php"\` | wc -l
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.