How to know last time `apt-get update` was executed?

Check the time stamp of /var/lib/apt/periodic/update-success-stamp.

$ ls -l /var/lib/apt/periodic/update-success-stamp
-rw-r--r-- 1 root root 0 Jan 25 01:41 /var/lib/apt/periodic/update-success-stamp

Here the time is Jan 25 01:41 when apt-get last executed. To get the time only, use the following command in terminal,

$ ls -l /var/lib/apt/periodic/update-success-stamp | awk '{print $6" "$7" "$8}'
Jan 25 01:41

It is the best place to check the last update time. If you found /var/lib/apt/periodic/ to be empty you can try,

ls -l /var/log/apt/history.log

Update

It is found that due to some reasons above files update-success-stamp or history.log remain unavailable in some systems. There is a new proposal from derobert to look into the file /var/cache/apt/pkgcache.bin.

pkgcache.bin is Apt's memory mapped package cache location. It get renewed after each update. So it is the perfect candidate to know the last time when apt was updated.

One can use the following command to know the exact time,

ls -l /var/cache/apt/pkgcache.bin | cut -d' ' -f6,7,8

or

stat /var/cache/apt/pkgcache.bin

You can check your command history in terminal :

history | grep 'apt update'

To check it by time :

HISTTIMEFORMAT="%d/%m/%y %T " history | grep '[a]pt update'

(The [a] part of the regular expression only matches the letter a but has the effect to not match itself when grepping in the history.)

enter image description here

Hope it helps !


I use /var/cache/apt to determine if I need to run apt-get update. By default, if the difference between the current time and cache time of /var/cache/apt is less than 24 hr, I don't need to run apt-get update. The default update interval can be overridden by passing a number to function runAptGetUpdate()

function trimString()
{
    local -r string="${1}"

    sed -e 's/^ *//g' -e 's/ *$//g' <<< "${string}"
}

function isEmptyString()
{
    local -r string="${1}"

    if [[ "$(trimString "${string}")" = '' ]]
    then
        echo 'true'
    else
        echo 'false'
    fi
}

function info()
{
    local -r message="${1}"

    echo -e "\033[1;36m${message}\033[0m" 2>&1
}

function getLastAptGetUpdate()
{
    local aptDate="$(stat -c %Y '/var/cache/apt')"
    local nowDate="$(date +'%s')"

    echo $((nowDate - aptDate))
}

function runAptGetUpdate()
{
    local updateInterval="${1}"

    local lastAptGetUpdate="$(getLastAptGetUpdate)"

    if [[ "$(isEmptyString "${updateInterval}")" = 'true' ]]
    then
        # Default To 24 hours
        updateInterval="$((24 * 60 * 60))"
    fi

    if [[ "${lastAptGetUpdate}" -gt "${updateInterval}" ]]
    then
        info "apt-get update"
        apt-get update -m
    else
        local lastUpdate="$(date -u -d @"${lastAptGetUpdate}" +'%-Hh %-Mm %-Ss')"

        info "\nSkip apt-get update because its last run was '${lastUpdate}' ago"
    fi
}

Sample Output:

<root@ubuntu><~/ubuntu-cookbooks/libraries>
# runAptGetUpdate 

Skip apt-get update because its last run was '0h 37m 43s' ago

I extracted these functions from my personal github: https://github.com/gdbtek/ubuntu-cookbooks/blob/master/libraries/util.bash

Tags:

Updates

Apt

Logs