linux - 使用 shell 绘制图形以获取总文件数和创建日期

标签 linux bash shell awk csh

我想创建一个直方图,Y 轴上的文件总数间隔为 50,X 轴上的创建时间以周为单位(即,如果新文件是在第 1 周和第 2 周之间创建的,依此类推)

有点像

在某一周内创建了 200、150、100、50 个文件 Y 轴上的 7、14、21、28 天。有点迷失了如何实现这一点。感谢任何帮助

更新:我正在尝试沿着这些方向

find <dirname> -type f -ctime -1 -ctime -7 | wc -l
find <dirname> -type f -ctime +7 -ctime -14 | wc -l

找到最大数并将其用作我的 X 轴上限。然后将这个数字分成相等的间隔来绘制我的 X 轴

最佳答案

这是将 GNU awk 用于时间函数的开始(未经测试,因为您没有提供我们可以测试的简洁、可测试的示例输入):

find "$1" -type f -printf '%T@ %p\0' |
awk -v RS='\0' '
BEGIN {
    nowSecs = systime()
}
{
    fileName     = gensub(/\S+\s+/,"",1)
    fileModSecs  = int($1)
    fileAgeSecs  = nowSecs - fileModSecs
    fileAgeDays  = int(fileAgeSecs / (24 * 60 * 60))
    fileAgeWeeks = int(fileAgeDays / 7)
    weekNr       = fileAgeWeeks + 1
    fileCnts[weekNr]++
    numWeeks     = (weekNr > numWeeks ? weekNr : numWeeks)
    maxFileCnt   = (fileCnts[weekNr] > maxFileCnt ? fileCnts[weekNr] : maxFileCnt)
    print nowSecs, fileModSecs, fileAgeSecs, fileAgeDays, fileAgeWeeks, weekNr, fileName | "cat>&2"
}
END {
    for (fileCnt=maxFileCnt; fileCnt>0; fileCnt--) {
        for (weekNr=1; weekNr<=numWeeks; weekNr++) {
            if (weekNr in fileCnts) {
                char[weekNr] = "*"
            }
            printf "%s%s", char[weekNr], (weekNr<numWeeks ? OFS : ORS)
        }
    }
    for (weekNr=1; weekNr<=numWeeks; weekNr++) {
        printf "%s%s", weekNr, (weekNr<numWeeks ? OFS : ORS)
    }
}
'

您需要找出 END 部分中循环的详细信息以打印直方图,但上面至少向您展示了如何在不多次调用查找和硬编码天数的情况下按周获取文件数一周又一周。

关于linux - 使用 shell 绘制图形以获取总文件数和创建日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51106352/

相关文章:

linux - Ubuntu Server VMware www 文件夹不工作

linux - bash one liner 的奇怪输出

linux - 在 bash 中读取 CSV 文件

shell - shell中多个文件的平均值

bash - 值未分配给 shell 脚本中的变量

linux - Shell 脚本段错误 - AWS

python - 从 Linux 终端运行 Python 脚本到 LocalHost

linux - 使用静态库配置测试

bash - 我如何在本地获取我在 Docker 格式环境文件中定义的环境变量?

bash - 将 bash 变量插入 perl 搜索和替换