How to extract rpm preamble

nolandda asked:

I am working on a non-redhat based machine and I need to extract Version: x.y.z and Release: a.b.c information from some .rpms that pass across the machine.

Typically one would do:

rpm -qip ./Foo.x.y.z.rpm | grep Version | awk ...

But this isn’t a redhat-based machine so I don’t have rpm and I don’t really want to install a 2nd package manager anyway.

I thought that if I did:

rpm2cpio ./Foo.x.y.z.rpm | cpio -idmv

Then when the cpio was extracted I would find some file named .preamble or something that would have the data in it. But no luck. Anyone know another way?

My answer:


Just go ahead and install rpm. It’s the easiest thing. Your distro almost certainly has it available to you.

Now you can use RPM query formats to extract the information you want.

For example:

file=/path/to/filename.rpm

name=$(rpm --queryformat "%{name}" -qp $file)
version=$(rpm --queryformat "%{version}" -qp $file)
release=$(rpm --queryformat "%{release}" -qp $file)

echo "This RPM named $name has version $version release $release"

View the full question and any other answers on Server Fault.

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