You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否有工具可展示文件夹/代码仓库内各文件的代码行数分布情况

代码仓库文件行数分布统计方案

方案1:基于cloc统计纯代码行分布(推荐)

cloc本身支持按单个文件输出代码行数(自动排除空行、注释行),配合简单的awk脚本即可得到区间分布:

  1. 首先导出所有文件的纯代码行数:
# 统计当前目录下所有代码文件的单文件行数,输出到临时文件
cloc --by-file --csv . | grep -v "header,language" | awk -F ',' '{print $5}' | grep -E '^[0-9]+$' > line_counts.txt
  1. 执行区间统计命令,直接输出你需要的分布表:
awk '
BEGIN {
    gap = 10 # 区间步长,可自行修改,比如改成20就是20行一个区间
    max_threshold = 200 # 超过该值统一归为XX+分类,可自行修改
    for (i=1; i<=max_threshold; i+=gap) {
        key = sprintf("%d-%d", i, i+gap-1)
        cnt[key] = 0
    }
    cnt[max_threshold"+"] = 0
}
{
    if ($1 > max_threshold) cnt[max_threshold"+"]++
    else {
        idx = int(($1-1)/gap)*gap + 1
        key = sprintf("%d-%d", idx, idx+gap-1)
        cnt[key]++
    }
}
END {
    printf "%-15s %s\n", "#lines of code", "#files"
    for (i=1; i<=max_threshold; i+=gap) {
        key = sprintf("%d-%d", i, i+gap-1)
        printf "%-15s %d\n", key, cnt[key]
    }
    printf "%-15s %d\n", max_threshold"+", cnt[max_threshold"+"]
}
' line_counts.txt

方案2:纯shell快速统计(包含空行、注释)

如果不需要排除空行和注释,仅统计文件物理总行数,不用安装cloc直接用系统自带命令即可:

# 可自行修改-name参数过滤需要统计的文件后缀,比如只统计.go和.rs文件就写 -name "*.go" -o -name "*.rs"
find . -type f \( -name "*.py" -o -name "*.js" -o -name "*.java" \) | xargs wc -l | grep -v "total" | awk '{print $1}' | sort -n > line_counts.txt
# 后续用上面同样的awk脚本统计区间分布即可

文本直方图生成

把上面awk脚本的END块替换为以下内容,即可直接输出文本形式的直方图:

END {
    printf "%-15s %-6s %s\n", "#lines of code", "#files", "histogram"
    # 计算最大数量用来适配直方图宽度
    max_cnt = 0
    for (k in cnt) if (cnt[k] > max_cnt) max_cnt = cnt[k]
    for (i=1; i<=max_threshold; i+=gap) {
        key = sprintf("%d-%d", i, i+gap-1)
        bar_len = max_cnt > 0 ? int(cnt[key]/max_cnt * 50) : 0
        bar = ""
        for (n=0; n<bar_len; n++) bar = bar "#"
        printf "%-15s %-6d %s\n", key, cnt[key], bar
    }
    key = max_threshold"+"
    bar_len = max_cnt > 0 ? int(cnt[key]/max_cnt * 50) : 0
    bar = ""
    for (n=0; n<bar_len; n++) bar = bar "#"
    printf "%-15s %-6d %s\n", key, cnt[key], bar
}

输出效果示例:

#lines of code   #files  histogram
1-10             12      ########
11-20            23      ###############
21-30            75      ##################################################
...
200+             3       ##

内容的提问来源于stack exchange,提问作者Jonathan N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 04:36:05