Detect if apt-get update is necessary

Is there a way to detect if running apt-get update is necessary?

It seems that whenever apt-get update is run, it downloads the latest lists even if I already have them, and I want to avoid downloading lists if I already have the latest ones.

8

2 Answers

You could extract the URLs from all sources in /etc/apt/sources.list and /etc/apt/sources.list.d/* and compare the file's timestamps in the respective remote repos against the ctimes of your files in /var/lib/apt/lists ... but that would IMHO take longer than just downloading those again (at least on my connection the 50MB are dealt with very quickly).

Your statement is not correct: apt update does not fetch catalogue files which it already has downloaded at the up-to-date versions; let's see:

  1. The first run of apt update:

    …
    Get:33 sid/main DEP-11 48x48 Icons [3,683 kB]
    Get:34 sid/main DEP-11 64x64 Icons [7,474 kB]
    Fetched 16.1 MB in 4s (4,496 kB/s)
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    64 packages can be upgraded. Run 'apt list --upgradable' to see them.

    As you can see, apt indeed fetched 16.1 MiB worth of data.

  2. The second run:

    Hit:1 buster/updates InRelease
    Hit:2 buster InRelease
    Hit:3 buster-updates InRelease
    Hit:4 sid InRelease
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    64 packages can be upgraded. Run 'apt list --upgradable' to see them.

    As you can see, no data were fetched.

"The trick" here is that when apt makes an HTTP[S] request to an archive server, it includes in it the "last modified" time of each resource it has alredy cached; the server looks at that timestamp, and if it does not have a version of that resource which is fresher, it responds with a special HTTP status code ("not modified") and no body. apt notices that and uses what's already available right away.

As you can see, there is still some traffic (outgoing requests and incoming responses to them) but:

  • It's miniscule compared to that when the data is fetched.
  • You simply can't be sure the archive have no updated data without performing a check.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like