Convert a RPM package to tarball

This might be a weired use case, but sometime one may want to extract all contents from an RPM and neatly put into a tarball.

If you are still reading, here comes the shell script to do the task.

#!/bin/sh

EXEC="=== "`basename $0`
PWD=`pwd`
RPMFILE=`readlink -f $1`

if [ -z "$2" ]; then
        TGZFILE=$PWD/`basename $1 | sed 's/rpm/tgz/g'`
else
        TGZFILE=$2
fi
TMPDIR=/tmp/$0-`whoami`

rm -rf $TMPDIR
mkdir -p $TMPDIR

echo "$EXEC: Changing to TMPDIR=$TMPDIR"
cd $TMPDIR

echo "$EXEC: Unpacking RPMFILE=$RPMFILE"
rpm2cpio $RPMFILE |cpio -idmv

echo "$EXEC: Generating zipped tarball TGZFILE=$TGZFILE"
tar -zcvf $TGZFILE *

echo "$EXEC: Done"

Sure there are many ways to improve, e.g., include a usage, but it’s working fine for me as it is.


Contents on this site are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Creative Commons License