Sort installed packages version

Antoine asked:

I’m trying to get last version of installed kernel package.
The main difficulty is that in my case the versions may be installed in a wrong order, so I cannot simply use the --last argument from rpm.

I wanted to use the coreutilssort command (coreutils-8.4-31.el6_5.1) which contains a --version-sort argument, but this does not return as expected:

$ rpm -q 'kernel' |sort --version-sort
kernel-2.6.32-431.5.1.el6.x86_64
kernel-2.6.32-431.11.2.el6.x86_64
kernel-2.6.32-431.17.1.el6.x86_64
kernel-2.6.32-431.el6.x86_64

rpm -q 'kernel' --last
kernel-2.6.32-431.17.1.el6.x86_64             mar. 13 mai 2014 09:48:44 CEST
kernel-2.6.32-431.11.2.el6.x86_64             jeu. 24 avril 2014 11:15:25 CEST
kernel-2.6.32-431.5.1.el6.x86_64              mer. 12 mars 2014 15:52:23 CET
kernel-2.6.32-431.el6.x86_64                  ven. 29 nov. 2013 17:48:19 CET

As you can see, the revision 431.e16 (the oldest) is sorted as the latest.
A call to rpm -qi confirm that:

Release     : 431.17.1.el6                  Build Date: jeu. 08 mai 2014 02:48:49 CEST
Release     : 431.el6                       Build Date: ven. 22 nov. 2013 05:28:33 CET

Is this a known bug of sort? Is it already patched? Is there a simple workaround or another way to get the latest installed version?

My answer:


You can do this by specifying a custom query format.

# rpm -q --qf="%{BUILDTIME} %{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}n" kernel

This will display the time the package was originally created (by Red Hat) as a UNIX timestamp followed by the package name.

To finish it off, all you need is…

# rpm -q --qf="%{BUILDTIME} %{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}n" kernel | 
  sort | tail -n 1 | cut -f 2 -d ' '

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.