How to extract RPM package without installing it

The file content of an RPM is usually installed. This means that the RPM files are copied onto their proper locations in the file system so that they may be used. For example, installing the bash binary RPM places the “/bin/bash” file into its expected location. To install an RPM, use either the “-i” or “-U” switch:

$ rpm -ivh bash-3.1-16.1.x86_64.rpm

Listing of the files inside an RPM package

You can get a listing of the files inside an RPM by performing an RPM query and adding the “-p” switch:

$ rpm -q -l -p bash-3.1-16.1.x86_64.rpm
/bin/bash
/bin/sh
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
...

How to Extract Files from a RPM Package File

Sometimes, you may want to obtain the content of, or a single file from, an RPM package. To do that, you can use the rpm2cpio conversion tool. The rpm2cpio tool extracts the content of a source or binary RPM in the form of a CPIO, not a TAR, archive. The rpm2cpio output is written to the standard output and usually piped into the cpio command.

1. Listing the files in a package file

In this example, we used the -t option to direct cpio to produce a “table of contents” of the archive created by rpm2cpio. This can make it much easier to get the right filename and path when you want to extract a file.

$ rpm2cpio bash-3.1-16.1.x86_64.rpm | cpio -t

Notice that the “rpm -qpl” command shows the filename as the absolute path “/bin/bash” but the “cpio -t” output uses the “./bin/bash” relative pathname instead. This is very handy because you can use rpm2cpio as an ordinary user to extract files that would normally install into privileged directories.

The following example shows how the output appears:

$ rpm2cpio bash-3.1-16.1.x86_64.rpm | cpio -t
./bin/bash
./bin/sh
./etc/skel/.bash_logout
./etc/skel/.bash_profile
./etc/skel/.bashrc
...
10510 blocks

2. Extracting one or more files from a package file

Use rpm2cpio to generate the CPIO archive, and use the cpio “-i” switch to extract the desired files:

$ rpm2cpio bash-3.1-16.1.x86_64.rpm | cpio -ivd ./bin/bash
./bin/bash
10510 blocks
$ ls -l /home/user/bin/bash
total 744
-rwxr-xr-x 1 user user 753720 March  1 13:10 bash

In this case, the options “-i“, “-v“, and “-d” direct cpio to:
-i – Extract one or more files from an archive. The filename(s) requested must match the “cpio -t” table of contents exactly, or a shell-like wildcard pattern. If the destination file already exists, cpio will refuse to overwrite it unless the “-u” switch is added to the command.
-d – Create any directories that precede the filename specified in the cpio command. If the directories already exist, the “-d” switch could be omitted but does no harm if it is always specified.
-v – Display the names of any files processed, along with the size of the CPIO archive file, in 512-byte blocks. Note this size is not the size of the RPM package, which cpio never sees directly.

If no filenames are given, then all the RPM files will be extracted.

Related Post