When running apt-get update, it downloads the package list and if there are upgradable packages it outputs something like

1 package can be upgraded. Run 'apt list --upgradable' to see it.

I could parse the output grepping for "can be upgraded" or something similar, but there must be a more elegant way to know if there are packages that be upgraded after doing an update.

I noticed that apt-get update outputs this information nearly immediately after downloading the data, while if i run apt list --upgradeable it takes a long time, presumably because it does not just tell me how many packages there are, but goes on to produce a full list of them, which I don't need. I just need to quickly know if there are any packages to update.

Note: an answer that requires to use a programming interface to apt to do this would be acceptable

  • 1
    As you already said apt update quickly tells you how many packages need upgrading. Later on you say this all you need to know. Could you clarify what exactly you do need? – WinEunuuchs2Unix Oct 9 at 15:18
  • I need this information available to other scripts, so that I can take action if there are packages that need upgrading. Grepping the output of apt update is doable but unsafe, so I am looking for alternatives. – feralgeometry Oct 9 at 15:28

The time it takes to list the items is short AFTER you have done a sudo apt update...

Example: (I forgot to add "time" to the first)...

$ apt list --upgradable
Listing... Done
libpython2.7/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
libpython2.7-dev/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
libpython2.7-minimal/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
libpython2.7-stdlib/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
libpython3.5/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
libpython3.5-dev/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
libpython3.5-minimal/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
libpython3.5-stdlib/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
python2.7/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
python2.7-dev/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
python2.7-minimal/xenial-security 2.7.12-1ubuntu0~16.04.9 amd64 [upgradable from: 2.7.12-1ubuntu0~16.04.8]
python3.5/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
python3.5-dev/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
python3.5-minimal/xenial-security 3.5.2-2ubuntu0~16.04.9 amd64 [upgradable from: 3.5.2-2ubuntu0~16.04.8]
hannu@wkbx ~ bash (P)PID=(19384)23069, s=0

$ time apt list --upgradable
Listing... Done
0.326 seconds elapsed, p=100.03, 0.326 real, 0.318 user, 0.008 sys.

So... this will give you $items set to the number of available updates:

$ items="$(( $(apt list --upgradable 2>/dev/null | grep upgradable | wc -l) - 1 ))" ; echo $items

Now, use bash conditionals to determine which commands to execute based on it.

  • www.tldp.org - read the guides on bash-programming to get started. – Hannu Oct 9 at 16:02
  • I say in the original question that apt list --upgradable takes a long time to produce that list, which is not good for me. While apt update nearly instantly tells me that there are packages that can be upgraded, which would be what I need if it did not require me to parse the output. – feralgeometry Oct 9 at 16:04
  • And I tell you above that it depends on what you have done just before it. apt list --upgradeable is quick after an update - I take it to be solely dependent on the communication speed to the server you happen to connect to. Something you will probably not be able to change much regardless of what you try. – Hannu Oct 9 at 16:24
  • Ok, I have done some more tests, and it turns out that the difference was because of the fact that when calling apt update I am asking it to just use a specific source list, for a repository where there are very few packages, and ignore the system ones. I did not mention this because I thought it would not make much of a difference, but it does – feralgeometry Oct 9 at 16:52
  • Nice one-liner +1 – WinEunuuchs2Unix Oct 9 at 22:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.