Wednesday 30 January 2013

Extracting single file from rpm/deb packages

   Sometimes rpm/deb package installation fails due to other dependent rpm/deb packages. It becomes very hectic to resolve all dependencies. But if you need some files from package and don't want pure installation of whole rpm then don't waste your time, you can use tools like rpm2cpio to extract single file from rpm package. I came across such need while using crash tool, which requires vmlinux file as one of the arguments. On Redhat this file is shipped with kernel-debuginfo-common-x.y.z.rpm package but to install it you may need to install other rpms because of dependencies between two. But you can extract required file by following way :

 Extracting file from rpm package:

For example if you want to extract contents of unifdef-1.171-5.fc6.x86_64.rpm then use following command


[pritam@pritam-pc]$ rpm2cpio unifdef-1.171-5.fc6.x86_64.rpm | cpio -idmv
./usr/bin/unifdef
./usr/bin/unifdefall.sh
./usr/share/man/man1/unifdef.1.gz
47 blocks

This will create required files in your *current directory*. If you want to extract single file lets say /usr/bin/unifdef in above case then use following command,

[pritam@pritam-pc]$ rpm2cpio unifdef-1.171-5.fc6.x86_64.rpm | cpio -idmv ./usr/bin/unifdef
./usr/bin/unifdef
47 blocks
 
This will only extract /usr/bin/unifdef in your *current directory* 


 Extracting file from deb package:

   DEB files are ar archives, which always contains the three files — debian-binary, control.tar.gz, and data.tar.gz. We can use ar command and tar command to extract and view the files from the deb package, as shown below.
For example we are extracting initramfs-tools.deb package

[pritam@pritam-pc]$ ar -vx initramfs-tools.deb
x - debian-binary
x - control.tar.gz
x - data.tar.gz

This will produce above three files in your *current directory*.
Then you can extract data.tar.gz to get required files

[pritam@pritam-pc]$ tar -xvzf data.tar.gz