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 ftp://rpmfind.net/linux/opensuse/factory/repo/oss/suse/x86_64/hello-2.9-4.3.x86_64.rpm
$ ls
hello-2.9-4.3.x86_64.rpm

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
/usr/bin/hello
/usr/share/doc/packages/hello
/usr/share/doc/packages/hello/ABOUT-NLS
/usr/share/doc/packages/hello/COPYING
/usr/share/doc/packages/hello/NEWS
/usr/share/doc/packages/hello/README
/usr/share/doc/packages/hello/THANKS
/usr/share/doc/packages/hello/TODO
/usr/share/info/hello.info.gz
/usr/share/locale/bg/LC_MESSAGES/hello.mo
/usr/share/locale/ca/LC_MESSAGES/hello.mo
/usr/share/locale/da/LC_MESSAGES/hello.mo
/usr/share/locale/de/LC_MESSAGES/hello.mo
/usr/share/locale/el/LC_MESSAGES/hello.mo
/usr/share/locale/eo/LC_MESSAGES/hello.mo
/usr/share/locale/es/LC_MESSAGES/hello.mo
/usr/share/locale/et/LC_MESSAGES/hello.mo
/usr/share/locale/eu/LC_MESSAGES/hello.mo
/usr/share/locale/fa/LC_MESSAGES/hello.mo
/usr/share/locale/fi/LC_MESSAGES/hello.mo
/usr/share/locale/fr/LC_MESSAGES/hello.mo
/usr/share/locale/ga/LC_MESSAGES/hello.mo
/usr/share/locale/gl/LC_MESSAGES/hello.mo
/usr/share/locale/he/LC_MESSAGES/hello.mo
/usr/share/locale/hr/LC_MESSAGES/hello.mo
/usr/share/locale/hu/LC_MESSAGES/hello.mo
/usr/share/locale/id/LC_MESSAGES/hello.mo
/usr/share/locale/it/LC_MESSAGES/hello.mo
/usr/share/locale/ja/LC_MESSAGES/hello.mo
/usr/share/locale/ko/LC_MESSAGES/hello.mo
/usr/share/locale/lv/LC_MESSAGES/hello.mo
/usr/share/locale/nb/LC_MESSAGES/hello.mo
/usr/share/locale/nl/LC_MESSAGES/hello.mo
/usr/share/locale/nn/LC_MESSAGES/hello.mo
/usr/share/locale/pl/LC_MESSAGES/hello.mo
/usr/share/locale/pt/LC_MESSAGES/hello.mo
/usr/share/locale/pt_BR/LC_MESSAGES/hello.mo
/usr/share/locale/ro/LC_MESSAGES/hello.mo
/usr/share/locale/ru/LC_MESSAGES/hello.mo
/usr/share/locale/sk/LC_MESSAGES/hello.mo
/usr/share/locale/sl/LC_MESSAGES/hello.mo
/usr/share/locale/sr/LC_MESSAGES/hello.mo
/usr/share/locale/sv/LC_MESSAGES/hello.mo
/usr/share/locale/th/LC_MESSAGES/hello.mo
/usr/share/locale/tr/LC_MESSAGES/hello.mo
/usr/share/locale/uk/LC_MESSAGES/hello.mo
/usr/share/locale/vi/LC_MESSAGES/hello.mo
/usr/share/locale/zh_CN/LC_MESSAGES/hello.mo
/usr/share/locale/zh_TW/LC_MESSAGES/hello.mo
/usr/share/man/man1/hello.1.gz

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!