Search code examples
linuxbashshellawkcsh

Plot graph using shell for total file count and date created


I want to create a histogram with total file count intervals of 50 on Y-axis and time created in weeks on X-axis (i.e if new files were created between week 1 and 2 and so on)

Something like

200, 150, 100 , 50 files created during a certain week 7, 14, 21, 28 days on Y-axis. Kind of lost on how to implement this. Any help is appreciated

Update: I am trying along these lines

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

Find the max number and use this as my X-axis upper limit. Then divide this number into equal intervals to plot my X-axis


Solution

  • Apologies being ksh instead of bash (bash level is near echo "Hello World") :)...

    Would that do what you need ?

        #!/bin/ksh
        ######################################
        #
        # statDirReport.sh
        #
        version="1.0"
        # Andre Gelinas, 2018
        #
        ######################################
    
        #############
        # Variables
        #############
    
        typeset -F2 SCALE
    
        # Max value of X
        X_SCALE=30
    
    
        #############
        # Main
        #############
    
        if [[ -n $1 ]]; then
            DIRNAME=$1
        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
    

    Edit to modify to add dates as title for the histograms. Modify the last part, right after the "DATA_2_SCALE" loop, with :

        #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