How To Extract an RPM Package Without Installing It (rpm extract command)

by Vivek Gite · 21 comments

Most of you may know to how extract a tarballs and/or a zip files. Someone recently PM me with a question:

How do I extract an RPM package without installing it on my Fedora Core Linux v5?

Extract rpm File

To be frank there is no direct RPM option available via rpm command to extract an RPM file. But there is a small nifty utility available called rpm2cpio. It Extract cpio archive from RPM Package Manager (RPM) package. With the following hack you will be able to extract an RPM file.

So rpm2cpio converts the .rpm file specified as a single argument to a cpio archive on standard out. If a - argument is given, an rpm stream is read from standard in.

Syntax is as follows:

rpm2cpio myrpmfile.rpm
rpm2cpio - < myrpmfile.rpm
rpm2cpio myrpmfile.rpm | cpio -idmv

Examples - Extract files from rpm

Download an RPM file:
$ mkdir test
$ cd test
$ wget http://www.cyberciti.biz/files/lighttpd/rhel4-php5-fastcgi/php-5.1.4-1.esp1.x86_64.rpm
Extract RPM file using rpm2cpio and cpio command:
$ rpm2cpio php-5.1.4-1.esp1.x86_64.rpm | cpio -idmvOutput:

/etc/httpd/conf.d/php.conf
./etc/php.d
./etc/php.ini
./usr/bin/php
./usr/bin/php-cgi
./usr/lib64/httpd/modules/libphp5.so
./usr/lib64/php
./usr/lib64/php/modules
....
.....
..
./var/lib/php/session
./var/www/icons/php.gif
19188 blocks

Output of rpm2cpio piped to cpio command (see how to use cpio) with following options:

  • i: Restore archive
  • d: Create leading directories where needed
  • m: Retain previous file modification times when creating files
  • v: Verbose i.e. display progress

Verify that you have extracted an RPM file in current directory:
$ ls Output:

etc  php-5.1.4-1.esp1.x86_64.rpm  usr  var

This is useful if you want to extract configuration file or other file w/o installing an RPM file.

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 1 trackback }

Smith Data Processing Services » Blog Archive » links for 2006-10-22
10.22.06 at 5:17 am

{ 20 comments… read them below or add one }

1 Ivan 12.13.06 at 10:19 am

You can also use alien package to convert rpm to tgz…

2 nixcraft 12.13.06 at 2:40 pm

Thanks for pointing out alien package to convert rpm to tgz.

3 Jared 01.30.08 at 5:11 pm

thanks…worked first time

4 Jared 01.30.08 at 5:12 pm

I love it when I search Google and find a good answer right away.

Thank you so much.

5 Kulbir Saini 05.25.08 at 3:06 pm

thanks a lot :)

6 Robert 06.27.08 at 3:07 am

I just found this page and it is great. One thing to note is that opensuse compresses the cpio part with lzma to decrease size so you have to do something like the following instead.

rpm2cpio file.rpm | lzma -d | cpio -idmv

This will extract the files for you.

7 ram 07.15.08 at 8:12 pm

Saved me a hole lot of time.

Thank you.

8 langda 07.22.08 at 9:02 am

thanks a lot..

9 Matthew 08.13.08 at 2:30 am

This is very good, however, I don’t think I see the installation script from inside the RPM. how do I get that? If I’m missing something, please let me know. Thank you!

10 vivek 08.13.08 at 10:03 am

@Matthew,

To display scripts you need to use rpm command as follows, this faq is about extracting file from rpm. In short type:
rpm -pq --scripts file.rpm
rpm -pq --scripts rsnapshot-1.3.0-1.noarch.rpm

Here is sample output from my rpm:

postinstall scriptlet (using /bin/sh):
#
# upgrade rsnapshot config file
#
RSNAPSHOT_CONFIG_VERSION=`/usr/bin/rsnapshot check-config-version`
if test $? != 0; then
        echo "Error upgrading /etc/rsnapshot.conf"
fi

if test "$RSNAPSHOT_CONFIG_VERSION" = "1.2"; then
        # already latest version
        exit 0
fi

if test "$RSNAPSHOT_CONFIG_VERSION" = "unknown"; then
        /usr/bin/rsnapshot upgrade-config-file
        RETVAL=$?
        exit $RETVAL
fi

HTH

11 Samuel Benjamin 08.22.08 at 12:29 pm

Excellent article that helped us save a lot of time from having to actually install a 64 bit linux system just to be able to retrieve the rpms for that platform.

I would suggest that the author remove the first two lines of the incomplete example which throws the user off until they realize that the cpio part has to be run together for actually extract the files.

No need to display :
rpm2cpio myrpmfile.rpm
rpm2cpio – < myrpmfile.rpm

(the above command dumps a continuous stream of unreadable characters to the screen)

Just need to know :
rpm2cpio myrpmfile.rpm | cpio -idmv

Thanks for sharing this with the linux community.

- Samuel Benjamin, NC.

12 Martijn 09.24.08 at 10:04 am

Thanks, just what I needed.

13 Ritobroto Ram 12.24.08 at 4:32 am

Just what I needed in the nick of time…

Thank You!!!

14 Sanjay 01.31.09 at 1:42 am

Its really very fantastic search i ever made.

15 Marcio Carneiro 02.27.09 at 10:33 pm

So, now I have the rpm opened and with all dirs.
Can I just mv to / and have program installed and running?
Yes, I know, if this works the rpm database is not updated.
But this works?

16 Balakumar 03.23.09 at 6:23 am

Thanks Vivek,
It is very Useful

17 amit 04.07.09 at 5:36 am

Thanks,
its very helpful :)

18 chetan 05.16.09 at 4:21 pm

cool nice post

19 Zoid 07.23.09 at 10:50 am

Hey, this worked great. I kept getting segmentation fault with plain old rpm and all I really needed from the rpm was some source files in a direction. :-)

20 Pete Wilson 08.10.09 at 7:34 am

Fantastic! The command line as you gave it worked perfectly: 100% correct, unlike the usual almost-correct posting that I have to debug. You saved me hours of work. Thank you!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: