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

Plot graph using shell for total file count and date created

我想创建一个直方图,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轴

抱歉是 ksh 而不是 bash(bash 水平接近回声 "Hello World"):)...

这会满足您的需要吗?

    #!/bin/ksh
    ######################################
    #
    # statDirReport.sh
    #
    version="1.0"
    # Andre Gelinas, 2018
    #
    ######################################

    #############
    # Variables
    #############

    typeset -F2 SCALE

    # Max value of X
    X_SCALE=30


    #############
    # Main
    #############

    if [[ -n  ]]; then
        DIRNAME=
    else
        print -n "Enter full path to stat : "; read DIRNAME
    fi

    if [[ ! -d $DIRNAME || ! -r $DIRNAME || ! -x $DIRNAME ]]; then
        print "ERROR - Directory unusable - Exiting"
        exit
    fi

    ## Getting the data

    CTIME1=1
    CTIME2=0
    for ((i=1;i<=4;i++)); do
        CTIME2=$(($i*7))
        FILE_COUNT[$i]=$(find $DIRNAME -type f -ctime +$CTIME1 -ctime -$CTIME2 | wc -l)
        #To find late on the max amount
        F_COUNT[${FILE_COUNT[$i]}]=${FILE_COUNT[$i]}
        #
        CTIME1=$CTIME2
    done

    #Doing some math

    ## Highest number of file
    MAX_COUNT=${F_COUNT[-1]}
    ## Find the value of each tick
    SCALE=$(($MAX_COUNT/$X_SCALE))

    ## Find the real length of the histogram for each week
    ## having the highest amount using full x scale (integer mathematics)

    for ((i=1;i<=4;i++)); do
        DATA_2_SCALE[$i]=$(((${FILE_COUNT[$i]}*$X_SCALE)/$MAX_COUNT))
    done

    # Getting the report

    typeset -L2 Col1
    typeset -L1 Col2
    typeset -L$(($X_SCALE+5)) Col3
    typeset -L5 Col4

    Col1="Wk"
    Col2=" "
    Col3="Data"
    Col4="Real"

    clear
    print "statDirReport v$version\tScale is #=$SCALE\n"
    print "$Col1$Col2$Col3$Col4\n"
    for ((i=1;i<=4;i++)); do
        Col1=$i
        Col2="|"
        graph=""
        Col4=${FILE_COUNT[$i]}
        for ((j=1;j<=${DATA_2_SCALE[$i]};j++)); do
            graph+="#"
        done
        Col3=$graph
        print "$Col1$Col2$Col3$Col4"
    done

编辑修改以添加日期作为直方图的标题。在 "DATA_2_SCALE" 循环之后修改最后一部分,使用 :

    #Setting the title of each histogram

    ## Finding how many sec since the beginning of time

    TODAY_SEC=$(date +"%s")

    ## Finding real date for find range

    SEC_PER_DAY=86400

    lastDate=$(date -u -d @"$TODAY_SEC" +"%m/%d")

    for ((i=1;i<=4;i++)); do
            firstDate=$(date -u -d @"$(($TODAY_SEC-(7*$i*$SEC_PER_DAY)))" +"%m/%d")
            WEEK[$i]=$firstDate" to "$lastDate" "
            lastDate=$firstDate
    done

    # Getting the report

    typeset -L15 Col1
    typeset -L1 Col2
    typeset -L$(($X_SCALE+5)) Col3
    typeset -L5 Col4

    Col1="Wk"
    Col2=" "
    Col3="Data"
    Col4="Real"

    clear
    print "statDirReport v$version\tScale is #=$SCALE\n"
    print "$Col1$Col2$Col3$Col4\n"
    for ((i=1;i<=4;i++)); do
            Col1=${WEEK[$i]}
            Col2="|"
            graph=""
            Col4=${FILE_COUNT[$i]}
            for ((j=1;j<=${DATA_2_SCALE[$i]};j++)); do
                    graph+="#"
            done
            Col3=$graph
            print "$Col1$Col2$Col3$Col4"
    done

在主目录上使用 feedgnuplot

dirname=~
e=0
for f in `seq 7 7 28` ; do 
    find "${dirname}" -type f -ctime +$e -ctime -$f | wc -l
    e=$f 
done 2> /dev/null | 
feedgnuplot  --terminal 'dumb 50,15' --with boxes --unset grid --exit

输出:

  5500 +-+-----+-------+------+-------+-----+-+   
  5000 +-+ *********   +      +       +     +-+   
  4500 +-+ *       *       ********         +-+   
  4000 +-+ *       *       *      *         +-+   
  3500 +-+ *       *       *      *         +-+   
  3000 +-+ *       *       *      *         +-+   
  2500 +-+ *       *********      *         +-+   
  2000 +-+ *       *       *      *         +-+   
  1500 +-+ *       *       *      *         +-+   
  1000 +-+ *   +   *   +   *  +   ********* +-+   
   500 +-+-********************************-+-+   
       0       1       2      3       4       5 

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

find "" -type f -printf '%T@ %p[=10=]' |
awk -v RS='[=10=]' '
BEGIN {
    nowSecs = systime()
}
{
    fileName     = gensub(/\S+\s+/,"",1)
    fileModSecs  = int()
    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 部分中用于打印直方图的循环的详细信息,但上面至少向您展示了如何在不多次调用 find 和硬编码数字的情况下按周获取文件数每周的天数。