Un - uncompress many archive formats
#!/bin/bash
# echo "un - extract archive in a new directory. Usage: un <archivefile>"
# (c) 198x-2005 Tero.Karvinen atSign iki.fi GNU General Public Licence
# 2006-01-24 Published on the web page, removed some unneeded uncompressors.
# 2006-01-16 Added lzop support (for hdup)
# 2005-10-02 Changed "unrar" to "rar x" because the program is better and syntax does not change.
# 2005-09-24 Deb support.
# 2005-05-24 Support .xpi mozilla extensions and jar.
# 2005-01-07 Adapt for sarge version of unrar. Improved directory handling.
# 2004-10-26 Fix: no longer lose part of version number when removing suffix. Third less code.
# 2003-09-09 Rewrote as original was lost. License GPL.
# 198x, 199x First version for (Digital?) unix
# requires: perl, unpackers
### unpack and it's helper functions
unpack() # unpack archive to a new directory
{
F=`echo $1|tr '[A-Z]' '[a-z]'`
case "$F" in
*.tar.gz) S=".tar.gz"; P="tar -zxvf";;
*.tgz) S=".tgz"; P="tar -zxvf";;
*.tar.bz2) S=".tar.bz2"; P="tar -jxvf";; # tested
*.tar.bzip2) S=".tar.bzip2"; P="tar -jxvf";;
*.zip) S=".zip"; P="unzip";;
#*.rpm) S=".rpm"; P="rpm2cpio TARGET |cpio -dvi";; # not working (yet)
*.tar) S=".tar"; P="tar -xvf";;
*.bz2) S=".bz2"; P="bunzip2 -d -v";; # .tar.bz2 is preferred over this
#*.gz) P="gunzip -d -v";;
*.arj) S=".arj"; P="unarj";;
*.lha) S=".lha"; P="lha x";;
*.ace) S=".ace"; P="unace";;
*.rar) S=".rar"; P="rar x";; # on some systems "unrar", others "unrar x"
*.cab) S=".cab"; P="cabextract";; # some .cab:s need unshield (apt-get install unshield)
#*.cpio) P="cpio -dvi" # what is the suffix?
*.Z) S=".Z"; P="uncompress";;
*.zoo) S=".zoo"; P="zoo -extract";;
*.chm) S=".chm"; P="chmextract";;
*.xpi) S=".xpi"; P="unzip";;
*.jar) S=".jar"; P="jar xvf";;
*.tnef) S=".tnef"; P="echo ms-tnef not yet supported, try: tnef";;
*.deb) S=".deb"; P="ar xv";;
*.lzo) S=".lzo"; P="lzop -x";;
*) echo " - not a known archive, skipping. " ;;
esac
DIR=`echo ""|perl -pe "s/$S//"`; # Currently not removing suffix with CAPS
if [ -e $DIR ]; then
DIR=$DIR-$RANDOM; # Should we allways add random number foo.zip-1231?
fi
if [ -e $DIR ]; then
echo " - $DIR allready exists, SKIPPING"
else
echo "### - Extracting with '$P' to dir \"$DIR\""
mkdir "$DIR"
cd "$DIR"
mv "../" .
$P "" |tr '\n' ' ' && echo ""
mv "" ..
cd ..
if [[ 1 -eq "`ls|wc -l`" ]]; then # wrongly assumes that it allways contains a dir
echo "removing unneeded nested directory"
EXTRADIR="`ls`"
cd "$EXTRADIR"
mv * ..
cd ..
rmdir "$EXTRADIR"
cd ..
fi
echo "# extracted to \"$DIR\""
echo ""
fi
}
### Main ###
if [ -z $1 ]; then
echo "un - extract archive in a new directory. Usage: un <archivefile>"
fi
PWD=`pwd`
while [ ! -z "" ]
do
if [ -d "" ]; then
echo " - directory, skipping"
else
unpack ""
DONE="$DONE ";
fi
cd $PWD;
shift
done
echo "### Processed: $DONE";
### (c)198x-2005 Tero.Karvinen atSign iki.fi GNU General Public Licence
### http://www.iki.fi/karvinen