Red Hat Package Manager files contain software ready for installation, and extracting them is your first step. Learning how to extract rpm file in Linux is a core skill for anyone managing software on Red Hat-based systems like Fedora, CentOS, or Rocky Linux. You might need to peek inside an RPM to see its contents, grab a specific file, or modify the package before installing it.
This guide walks you through every method, from simple commands to advanced tools. We’ll keep it practical and hands-on, so you can start extracting files right away.
How To Extract Rpm File In Linux
Extracting an RPM file means unpacking its contents without installing the software. This is useful for inspecting scripts, grabbing configuration files, or repackaging. The core tool is the rpm2cpio command, which converts the RPM into a cpio archive that you can then extract.
Let’s start with the most common approach. You’ll need a terminal and basic command-line skills. First, make sure you have the RPM file you want to extract. For example, let’s say you have a file called example-1.0-1.x86_64.rpm in your current directory.
Method 1: Using Rpm2cpio And Cpio
This is the standard method and works on almost all Linux distributions. The rpm2cpio tool is usually included with the rpm package itself.
- Open your terminal.
- Navigate to the folder containing the RPM file:
cd /path/to/your/rpm - Run this command:
rpm2cpio example-1.0-1.x86_64.rpm | cpio -idmv - Press Enter. The files will be extracted into the current directory.
The cpio options mean: -i for extract, -d to create directories as needed, -m to preserve file modification times, and -v for verbose output so you can see what’s happening.
If you only want to list the contents without extracting, use: rpm2cpio example.rpm | cpio -it. This shows you all files inside the package.
What If Rpm2cpio Is Not Installed?
On some minimal systems, you might need to install it. On Fedora or CentOS, run: sudo dnf install rpm2cpio. On older systems, sudo yum install rpm2cpio works. It’s a small utility and installs quickly.
Method 2: Using The Rpm2Archive Script
Some distributions include a script called rpm2archive. This converts the RPM directly into a tar archive, which is easier to handle for some users.
- Run:
rpm2archive example.rpm - This creates a file named
example.rpm.tarin the same directory. - Then extract the tar file:
tar -xvf example.rpm.tar
This method is simpler if you’re more comfortable with tar. The downside is it creates an extra intermediate file, so you need twice the disk space temporarily.
Method 3: Using 7-Zip Or File Roller (GUI)
If you prefer a graphical interface, you can use archive managers. On GNOME, File Roller (also called Archive Manager) can open RPM files directly.
- Right-click the RPM file.
- Select “Open With Archive Manager” or “Extract Here.”
- The GUI shows the contents. You can extract all or drag out specific files.
For command-line lovers, 7z (from p7zip) also handles RPMs: 7z x example.rpm. This extracts the cpio archive inside, but you may need to extract that cpio file again. It’s a two-step process.
Understanding The Structure Of An RPM File
Before you extract, it helps to know what’s inside. An RPM file is actually a combination of metadata and a compressed cpio archive. The metadata includes the package name, version, dependencies, and scripts. The cpio archive holds the actual files to be installed.
When you run rpm2cpio, it strips away the metadata and gives you just the cpio part. That’s why you pipe it to cpio for extraction. The files inside are laid out as they would be on the filesystem, so you’ll see paths like ./usr/bin/ or ./etc/.
Extracting Specific Files From An RPM
Sometimes you don’t want the whole package, just one file. You can do this with cpio and pattern matching.
- First, list the contents:
rpm2cpio example.rpm | cpio -it | grep filename - Once you know the exact path, extract only that file:
rpm2cpio example.rpm | cpio -idv ./path/to/file
For example, to extract just the README file: rpm2cpio example.rpm | cpio -idv ./usr/share/doc/example/README. The leading dot is important; it tells cpio to extract relative to the current directory.
Using Rpmunpack For A Simpler Workflow
There’s a lesser-known tool called rpmunpack that does the extraction in one step. It’s not always pre-installed, but you can get it from the rpmdevtools package.
Install it with: sudo dnf install rpmdevtools. Then simply run: rpmunpack example.rpm. It extracts everything into a subdirectory named after the package. This is great for keeping things organized.
Extracting RPMs On Non-Red Hat Systems
You might be on Ubuntu or Debian and still need to extract an RPM file. The same rpm2cpio method works, but you need to install the rpm package first.
On Ubuntu: sudo apt install rpm. Then follow the same steps as above. The rpm2cpio command will be available after installation. This is handy when you download an RPM for a tool you want to examine but can’t install directly.
Extracting With Alien
Another option on Debian-based systems is alien, which converts RPMs to DEB packages. But you can also use it to just extract files.
- Install alien:
sudo apt install alien - Run:
alien --to-tgz example.rpm - This creates a tar.gz file. Then extract that:
tar -xzf example.tgz
Alien is slower and adds an extra conversion step, so it’s not ideal for quick extraction. Stick with rpm2cpio when possible.
Common Issues And Troubleshooting
Sometimes things go wrong. Here are typical problems and fixes.
- Permission denied: Make sure you have read access to the RPM file. Use
ls -lto check. If needed,sudo chmod +r example.rpm. - Command not found: Install the required package as described above.
- Corrupt RPM file: Try downloading the file again. You can verify integrity with
rpm -K example.rpmif you have the GPG key. - Extracted files in wrong location: Always run the extraction from the directory where you want the files. The paths inside the RPM are absolute, so extracting in
/tmpis safer.
Extracting To A Specific Directory
By default, files are extracted to the current directory. To put them elsewhere, use the -D option with cpio.
Example: rpm2cpio example.rpm | cpio -idv -D /tmp/extracted. This places all files under /tmp/extracted while preserving the internal directory structure.
Automating Extraction With Scripts
If you need to extract many RPMs regularly, write a simple script. Here’s a bash example:
#!/bin/bash
for rpm in *.rpm; do
dir="${rpm%.rpm}"
mkdir -p "$dir"
cd "$dir"
rpm2cpio "../$rpm" | cpio -idmv
cd ..
done
This creates a folder for each RPM and extracts its contents there. Save it as extract-all.sh, make it executable with chmod +x extract-all.sh, and run it in the directory containing your RPM files.
Why Extract Instead Of Install?
You might wonder why not just install the package. Here are common reasons:
- You want to inspect the installation scripts (pre/post install).
- You need a specific binary or library without cluttering your system.
- You’re packaging software and need to examine how others do it.
- The RPM is for a different architecture or distribution version.
- You want to modify a configuration file before installation.
Extracting gives you full control without affecting your system’s package database.
Security Considerations
Always verify the source of your RPM files. Extracting a malicious package can still expose you to harmful scripts or binaries. Use rpm -K to check signatures if available. Never extract RPMs from untrusted sources as root; use a regular user account.
After extraction, scan the files with antivirus or at least review the scripts in the ./usr/lib/rpm/ or ./etc/ paths. The pre-install and post-install scripts are often in a file called scripts inside the cpio archive.
Advanced: Extracting Without Rpm2cpio
In rare cases where rpm2cpio is unavailable and you can’t install it, you can manually extract the cpio archive. RPM files are compressed with gzip or xz. You can decompress them and then extract the cpio part.
- First, identify the compression:
file example.rpm. It might say “gzip compressed data.” - Decompress:
zcat example.rpm > example.cpio(for gzip) orxzcat example.rpm > example.cpio(for xz). - Then extract:
cpio -idmv < example.cpio.
This is messy and not recommended, but it works in a pinch. The rpm2cpio tool handles the header parsing automatically, so it's much simpler.
Extracting Source RPMs (SRPMs)
Source RPMs have the extension .src.rpm and contain the source code and spec file. Extracting them is similar, but the contents are different.
Use the same rpm2cpio method: rpm2cpio example.src.rpm | cpio -idmv. You'll get a .spec file and a tarball with the source code. This is useful for rebuilding packages or studying how they're built.
Extracting SRPMs With Rpmbuild
If you have rpmbuild installed, you can use: rpm -i example.src.rpm. This places the files in ~/rpmbuild/SOURCES/ and ~/rpmbuild/SPECS/. It's a cleaner method if you plan to rebuild the package.
Comparing Extraction Methods
Here's a quick comparison to help you choose:
- rpm2cpio + cpio: Fast, universal, no extra files. Best for most cases.
- rpm2archive: Creates a tar file, good if you prefer tar.
- 7-Zip: Good for GUI users, but may need extra steps.
- Alien: Only on Debian-based systems, slower.
- rpmunpack: One-step extraction, neat organization.
For daily use, stick with rpm2cpio | cpio -idmv. It's the standard and works everywhere.
Practical Example: Extracting A Real RPM
Let's walk through a full example. Suppose you downloaded nginx-1.24.0-1.fc38.x86_64.rpm.
- Open terminal and go to Downloads:
cd ~/Downloads - List contents without extracting:
rpm2cpio nginx-1.24.0-1.fc38.x86_64.rpm | cpio -it | head -20 - Extract all:
rpm2cpio nginx-1.24.0-1.fc38.x86_64.rpm | cpio -idmv - Check the extracted files:
ls -la ./usr/sbin/ - You'll see the nginx binary and other files in their respective directories.
Now you can copy the binary elsewhere or examine the configuration files in ./etc/nginx/.
Cleaning Up After Extraction
After you're done, you might want to remove the extracted files. Simply delete the directory: rm -rf ./usr ./etc ./var (be careful not to delete real system files). It's safer to extract into a dedicated folder like /tmp/rpm-extract/.
To clean up completely: rm -rf /tmp/rpm-extract. This prevents accidental overwrites of system files.
Frequently Asked Questions
Can I extract an RPM file without installing it?
Yes, that's exactly what this guide covers. Use rpm2cpio and cpio to extract files without installing the package.
What is the difference between extracting and installing an RPM?
Extracting only unpacks the files to a directory. Installing runs scripts, updates the package database, and places files in system locations. Extraction is safer for inspection.
How do I extract an RPM file on Ubuntu?
Install the rpm package with sudo apt install rpm, then use rpm2cpio as described above. You can also use alien but it's slower.
Can I extract a single file from an RPM?
Yes, list the contents with rpm2cpio file.rpm | cpio -it | grep filename, then extract that specific path with rpm2cpio file.rpm | cpio -idv ./path/to/file.
Why do I get "cpio: premature end of file" error?
This usually means the RPM file is corrupt or incomplete. Re-download the file. Also check that you're using the correct command syntax.
Extracting RPM files is a straightforward process once you know the tools. Whether you're a system administrator, developer, or curious user, these methods give you full access to the contents of any RPM package. Practice with a few files, and you'll be comfortable in no time.
Remember to always extract in a safe directory and verify the source of your RPMs. Happy extracting!