Short Tip: Extract Files from a RPM file

shell.png
Sometimes it is necessary to extract a file from a RPM without actually installing it. This way you can extract for example binaries and check them for any malicious content before you install them to important system places. In case of src.rpms you can also directly extract a patch or spec in case you need it for your own RPM.

The tool you need in such a case is rpm2cpio. It converts an rpm file to a regular cpio archive.
In case you want to access a specific file you first have to list the entries of the file and afterwards extract them:

$ rpm2cpio coreutils-6.9-2.fc7.i386.rpm |cpio -t
./bin/basename
./bin/cat
./bin/chgrp
./bin/chmod
[...]
$ rpm2cpio coreutils-6.9-2.fc7.i386.rpm |cpio -ivd ./bin/basename
./bin/basename
16525 blocks
$ ls
bin coreutils-6.9-2.fc7.i386.rpm

As you see the file is extracted together with the directory structure, in this case /bin/.

As a side note, you can of course also get the lsit of files in a package by rpm -qpl whatever.rpm.

Speaking about rpm: if I had a command and wanted to know which package delivers it I always issued rpm -q --whatprovides `which basename`, but today learned that rpm -qf `which basename` works just fine. You never stop learning.