Background
Recently on the StackExchange site Unix & Linux the following question came up which seemed easy enough to answer.
This turned out to be trickier than I originally thought. What follows is my ultimate answer along with several failed attempts. I leave them so that others can learn from my mistakes.
Solution
TL;DR
The 3rd attempt is what ultimately worked! I’m leaving the first 2 attempts so that others that may come across this post in the future will hopefully gain some insight into how non-trivial a problem it is to parse RPM version information and determine the lineage of which came first, second, etc.
Attempt #1 (didn’t work)
This command will sort the output and give you them in version order:
1 2 3 4 5 |
$ rpm -q kernel --queryformat "%{VERSION} %{RELEASE}\n"|sort -n 2.6.18 238.12.1.el5 2.6.18 238.19.1.el5 2.6.18 274.12.1.el5 2.6.18 308.8.2.el5 |
WHY IT DIDN’T WORK: A naive person would think that you can use some variant of the sort
command to perform this task, but there is enough variability and inconsistency in the formatting of the actual version information for a given RPM that it just isn’t up to the task.
Attempt #2 (didn’t work)
1 2 |
$ rpm -q --last kernel | head -n 1 | cut -d' ' -f1 kernel-2.6.35.14-106.fc14 |
WHY IT DIDN’T WORK: I had high hopes that this approach would yield the results, but the issue with this one as was pointed out to me afterwards, is that the --last
switch is merely returning the results sorted by the date the RPMs were installed.
Attempt #3
This one will definitely do the job. I found a suite of tools called RPM Development Tools. There are 2 tools in this suite that will give you the capability to determine whether one version of a RPM is newer or older than another.
If the RPM isn’t already installed you can do so as follows:
1 |
yum install rpmdevtools
|
The first tool that’s useful is called rpmdev-vercmp
. This tool can compare 2 names of RPMs and tell you which one is newer. For example:
1 2 |
$ rpmdev-vercmp kernel-2.6.35.14-100.fc14.x86_64 kernel-2.6.35.14-103.fc14.x86_64
0:kernel-2.6.35.14-103.fc14.x86_64 is newer
|
After finding this I was all set to put together a shell script but then realized, man I’m lazy, so I poked a few minutes more and found another tool in the suite called rpmdev-sort
.
This tool is pay-dirt. You can use it as follows:
1 2 3 4 |
$ rpm -q kernel | rpmdev-sort kernel-2.6.35.14-100.fc14.x86_64 kernel-2.6.35.14-103.fc14.x86_64 kernel-2.6.35.14-106.fc14.x86_64 |
There are a lot of tools in RPM Development Tools that might be worth a look for others so I’m listing them here for future reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ rpm -q --queryformat '[%{NAME} %{FILEMODES:perms} %{FILENAMES}\n]' rpmdevtools \ | grep -E "^.* -..x..x..x " \ | awk '{print $3}' \ | sed 's#/usr/bin/##' \ | paste - - - \ | column -t annotate-output checkbashisms licensecheck manpage-alert rpmargs rpmdev-bumpspec rpmdev-checksig rpmdev-cksum rpmdev-diff rpmdev-extract rpmdev-md5 rpmdev-newinit rpmdev-newspec rpmdev-packager rpmdev-rmdevelrpms rpmdev-setuptree rpmdev-sha1 rpmdev-sha224 rpmdev-sha256 rpmdev-sha384 rpmdev-sha512 rpmdev-sort rpmdev-sum rpmdev-vercmp rpmdev-wipetree rpmelfsym rpmfile rpminfo rpmls rpmpeek rpmsodiff rpmsoname spectool |
An alternative to #3
An alternative that the OP mentioned in the comments is to use sort -V
. That’s a capital -V@. I'd never heard of this switch either. From the @sort
man page:
-V, –version-sort
natural sort of (version) numbers within text
As it turns out sort
does provide a facility to sort version numbers so you could also perform the sorting like so:
1 2 3 4 |
$ rpm -q kernel | sort -V kernel-2.6.35.14-100.fc14.x86_64 kernel-2.6.35.14-103.fc14.x86_64 kernel-2.6.35.14-106.fc14.x86_64 |