SoFunction
Updated on 2025-03-10

Shell script decompress rpm package

Sometimes you need to extract files from the RPM package, but you don’t install and don’t want to install rpm-related libraries and programs. At this time, the following small green script can help you achieve your wish.

Note:
1. This script comes from the Internet. I don’t remember the specific location. I have made a little changes to the source script.
2. The system needs to install the cpio program (usually the system brings it)

#!/bin/sh

#*************************************************************
#     Author: Unknown, Modified by 
#    Filename: 
#  Description: extrac rpm package without install 
#         rpm related program
#*************************************************************

if [ $# -lt 1 ] ; then
	echo -e "anUsage: `basename $0` <rpm_pkg_file> n"
	exit 1
fi

pkg=$1
if [ ! -e "$pkg" ]; then
  echo -e "nERR: package $pkg does not exist ! n"
	exit 1
fi

### check if cpio exists
CPIO_VER=`cpio --version 2>/dev/null`
if [ -z "$CPIO_VER" ]; then
	echo -e "nERR: prerequisite program cpio does not found ! n"
	exit 1
fi

leadsize=96
o=`expr $leadsize + 8`
set `od -j $o -N 8 -t u1 $pkg`
il=`expr 256 * ( 256 * ( 256 * $2 + $3 ) + $4 ) + $5`
dl=`expr 256 * ( 256 * ( 256 * $6 + $7 ) + $8 ) + $9`
# echo "sig il: $il dl: $dl"

sigsize=`expr 8 + 16 * $il + $dl`
o=`expr $o + $sigsize + ( 8 - ( $sigsize % 8 ) ) % 8 + 8`
set `od -j $o -N 8 -t u1 $pkg`
il=`expr 256 * ( 256 * ( 256 * $2 + $3 ) + $4 ) + $5`
dl=`expr 256 * ( 256 * ( 256 * $6 + $7 ) + $8 ) + $9`
# echo "hdr il: $il dl: $dl"

hdrsize=`expr 8 + 16 * $il + $dl`
o=`expr $o + $hdrsize`

dd if=$pkg ibs=$o skip=1 2>/dev/null | gunzip | cpio -idmuv || dd if=$pkg ibs=$o skip=1 2>/dev/null | bzip2 -d | cpio -idmuv

How to use: <RPM file>