Script to fetch and show your GNU/Linux distro age in days

Script to fetch and show your GNU/Linux distro age in days
I’d like to share a quick and dirty script written in BASH which is supposed to fetch and show the age of your GNU/Linux distribution in days. I created it just for fun as I thought it would be nice to get this information from somewhere and place it for example in your conky, or run it via command line or maybe you just want to know how old your distro is… anyhow, below is how I’m using this tool to get the age of my Gentoo Killer in my conky.

It is coded for and tested on Arch Linux, Gentoo Linux and Debian, but it should work just fine in Ubuntu as well as in CentOS and Fedora. The distroAge tool calculates the age of your Linux distribution by getting the most initial date logged by the distro’s package manager and then converts and calculates the time in between in days.


Here is the script:

#!/bin/bash
########################
## get your distro age in days
## author: Tom
## url: https://vpsineu.com/blog
########################

#####################
## FUNCTIONS and STUFF ##
#####################

###{{{ DETECT DISTRO
## tested on (currently): arch linux, gentoo, debian
detectDistro()
{
    local infoFileRegeX="/etc/(.*)[-_]"
    local infoDataPattern="Description:[[:space:]]*([^ ]*)"

    if [[ $(which lsb_release 2>/dev/null) ]]; then
        Info=$(lsb_release -d)
        if [[ ${Info} =~ ${infoDataPattern} ]]; then
            osName=${BASH_REMATCH[1]}
        else
            ## bad match in lsb release
            exit 1
        fi
    else
        etcFiles=$(ls /etc/*[-_]{release,version} 2>/dev/null)
        for file in ${etcFiles}; do
            if [[ ${file} =~ ${infoFileRegeX} ]]; then
                osName=${BASH_REMATCH[1]}
                break
            else
                ## dont know what else to do...
                exit 1
            fi
        done
    fi

    declare -i shellVer
    shellVer=$(printf ${BASH_VERSION}|cut -d. -f1)
    if [[ ${shellVer} -lt 4 ]]; then
        osName=$(printf ${osName}|tr [A-Z] [a-z])
    else
        osName=${osName,,}
    fi


    printf ${osName}
}
###}}} END DETECT DISTRO

###{{{ CONVERT MONTH FROM FORMAT FROM "%a" TO "%d" ex.( "Nov" to "11" )
convertMonth ()
{
    local months=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
    local count

    for (( count=0; count<11; count++ )); do
        [[ $1 = ${months[$count]} ]] && break
    done

    local finalOut=$(( count+1 ))
    [[ ${#finalOut} -lt 2 ]] && finalOut="0${finalOut}"
    printf ${finalOut}
}
###}}}

###{{{ covert and return the initial date
finalDate()
{   
    # init the year
    local year=$3
    [[ -z ${year} ]] && year=$(date +%Y)

    # init the day and fix it
    local day=$2
    [[ ${#day} -lt 2 ]] && day="0${day}"

    local month=$(convertMonth $1)
    printf "${year}-${month}-${day}"
}
###}}}

finalResult()
{   
    ## exit if something is wrong
    [[ $# -ne 1 ]] && printf "finalResult: many args" && exit 1
    ! [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && printf "wrong format" && exit 1

    local startDate=$(date --utc --date "${1}" +%s)
    local endDate=$(date --utc --date "$(date +'%Y-%m-%d')" +%s)
    ## calulate the difference between the two dates in days
    #distroAGE=$(( (endDate-startDate)/(3600*24) ))
    printf "%-d %-s" "$(( (endDate-startDate)/(3600*24) ))" "day(s)"
}

###{{{ DISTRO SPECIFIC FUNCTIONS
archLinux()
{
    printf "%-d %-s" "$(($(($(date +%s) - $(date -d "$(head -1 /var/log/pacman.log | cut -d ' ' -f 1,2 | tr -d '[]')" +%s))) / 86400))" "day(s)"
}

gentooLinux()
{
    IFS='
    '
    local aRray=($(ls -lt /var/log/portage/|tail -1|grep -Eo '[A-Z]{1}[a-z]{2}(.?){1,2}[0-9]{1,2}((.?){1,2}[0-9]{4}|)'|sed 's# \+#\n#g'))
    local installDATE=$(finalDate ${aRray[0]} ${aRray[1]} ${aRray[2]})
    finalResult ${installDATE}
}

debianLinux()
{
    local oldestAptFile=/var/log/apt/$(ls -lt /var/log/apt/|tail -1|awk '{print $NF}')
    local installDATE=$(zgrep 'Log started' ${oldestAptFile}|head -1|grep -Eo '[0-9]{4}-[0-9]{2}-[0-9]{2}')
    finalResult ${installDATE}
}

centosLinux()
{
    local oldestYumFile=$(ls -1t /var/log/yum*|tail -1)
    IFS='
    '
    local aRray=($(head -1 ${oldestYumFile}|grep -Eo '[A-Z]{1}[a-z]{2}(.?){1,2}[0-9]{1,2}((.?){1,2}[0-9]{4}|)'|sed 's# \+#\n#g'))
    local installDATE=$(finalDate ${aRray[0]} ${aRray[1]} ${aRray[2]})
    finalResult ${installDATE}
}

###}}} END DISTRO SPECIFIC

#################
## END FUNCTIONS ##
#################


### MAIN ###

os=$(detectDistro)
case "${os}" in
    gentoo )
        gentooLinux
        exit 0
     ;;
    arch )
        archLinux
        exit 0
     ;;
    debian|ubuntu )
        debianLinux
        exit 0
     ;;
    centos|fedora )
        centosLinux
        exit 0
     ;;
    * )
        exit 1
    ;;
esac

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>