How to extract files from RPM package archive

This short article will show you how to extract a single or multiple files from a RPM package archive. To begin we first download a sample package hello.

$ wget
$ ls

Now we have an option extract all or a single file from the above RPM package archive. The following linux command will extract all file into our current working directory:

$ rpm2cpio hello-2.9-4.3.x86_64.rpm | cpio -id
525 blocks
$ ls
hello-2.9-4.3.x86_64.rpm  usr

The above rpm2cpio first converts the RPM to CPIO archive. We then pass its STDOUT to a cpio command which takes it as an input and extracts its content -i into appropriate directories -d. As a result the usr directory now contains all extracted files from the hello-2.9-4.3.x86_64.rpm RPM package archive.

Alternatively, we can extract a single/selected file from the RPM package archive. To do this we first need to list a content of the RPM package without extracting its content. The bellow command will only simply list a content of the RPM package hello-2.9-4.3.x86_64.rpm

$ rpm -lqp hello-2.9-4.3.x86_64.rpm
warning: hello-2.9-4.3.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 3dbdc284: NOKEY

Use the following linux command to extract a single file /usr/bin/hello from RPM package and save it into eg. /tmp/ directory:

$ rpm2cpio hello-2.9-4.3.x86_64.rpm | cpio -iv --to-stdout ./usr/bin/hello > /tmp/hello

All done the hello binary executable has now been saved into /tmp/ directory:

$ chmod +x /tmp/hello
$ /tmp/hello 
Hello, world!