How to Extract Files from an RPM Package Archive

RPM (red hat package manager) is a package management system used primarily in Red Hat-based distributions like RHEL, CentOS, and Fedora. Sometimes, you may need to extract files from an RPM package without installing it, either for inspection or to use certain files in a different context. This article will guide you through the steps to extract files from an RPM package archive.

In this tutorial you will learn:

  • How to download an RPM package using dnf
  • How to create a directory for the package
  • How to extract files from an RPM package using rpm2cpio and cpio
How to Extract Files from an RPM Package Archive
How to Extract Files from an RPM Package Archive
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux-based system (preferably Red Hat-based distributions)
Software rpm2cpio, cpio, dnf
Other None
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Step-by-Step Guide to Extracting Files from an RPM Package Archive

Extracting files from an RPM package can be achieved using rpm2cpio and cpio. Here is a detailed step-by-step guide on how to perform this extraction.

  1. Create a directory for the package: First, create a directory to store the RPM package and navigate into it.
    $ mkdir wget
    $ cd wget/

    This command creates a directory named wget and changes the current working directory to it.

  2. Download the RPM package using dnf: Use the dnf command to download the RPM package. Note that this does not require root privileges.
    $ dnf download wget

    This command downloads the wget RPM package to the current directory. It may display a message about not updating Subscription Management repositories, which can be ignored.

  3. List the contents of the directory: Verify that the RPM package has been downloaded.
    $ ls

    This command lists the contents of the current directory, showing the downloaded eg. wget-1.21.1-7.el9.x86_64.rpm file.



  4. Convert and extract the RPM package: Use the rpm2cpio and cpio utilities to extract the contents of the RPM package.
    $ rpm2cpio wget-1.21.1-7.el9.x86_64.rpm | cpio -id

    This command converts the RPM package to a CPIO archive and then extracts its contents. The blocks message indicates successful extraction.

  5. Verify the extracted files: List the contents of the directory again to see the extracted files.
    $ ls

    This command lists the contents of the current directory, which now includes the extracted directories etc, usr, and the original RPM package file.

    Step-by-Step Guide to Extracting Files from an RPM Package Archive
    Step-by-Step Guide to Extracting Files from an RPM Package Archive

Conclusion

By following these steps, you can extract files from an RPM package archive without needing to install the package. This can be particularly useful for inspecting the contents of a package or for using certain files in a different context. The rpm2cpio and cpio utilities provide a straightforward method for this process, ensuring you can access the files you need efficiently.