1.统计当前目录及其子目录下的代码行数
find . "(" -name "*.java" -or -name "*.ymal" -or -name "*.properties" -or -name "*.xml" ")" -print | xargs grep -v "^$" | wc -l
整个命令的作用是:在当前目录及其子目录下,查找所有 .java、.ymal(或应为 .yaml)、.properties、和 .xml 文件,然后统计这些文件中非空行的总数 。
1. find . : 从当前目录(.)开始搜索。
2. "(" : 开始构造一个括号表达式来组合多个查找条件。
3. -name "*.java" -or -name "*.ymal" -or -name "*.properties" -or -name "*.xml" : 查找扩展名为 .java、.ymal、.properties、或 .xml 的文件。
4. )" : 结束括号表达式。
5. -print : 打印出所有匹配到的文件路径。
6. | : 管道符号,将上一个命令的输出作为下一个命令的输入。
7. xargs grep -v "^$" : xargs 接收来自 find 的输出(即文件路径列表),并将其作为参数传递给 grep 命令。grep -v "^$" 的作用是筛选出不包含空行(即行首至行尾无任何字符)的行。8. -v 是反向选择,即排除匹配的行,"^$" 正则表达式匹配空行。
9. | wc -l : 再次使用管道,将 grep 输出的行传递给 wc 命令,wc -l 用来计算行数,即统计不为空的总行数。
2. 统计项目内所有代码行数
git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
输出结果: added lines: 165091, removed lines: 40765, total lines: 124326
3. 统计项目内指定时间的代码行数
git log --since=2022-01-01 --until==2022-12-30 --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END {printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
输出结果: added lines: 66901, removed lines: 19314, total lines: 47587
4. 统计项目内指定时间指定作者的代码行数
git log --since==2022-12-29 --until==2022-12-30 --author="irving" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END {printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
输出结果:added lines: 26, removed lines: 20, total lines: 6