Wednesday, February 28, 2007

unpacker

Made an unpacker following a discussion on irc://irc.freenode.net/#fedora (or was it #kde?) that there's no good automatic unpacking tool for the CLI. Sure, you have ark etc. for the GUI, but you dont have an all-in-one utility for the CLI. So, here's the code for the thing. Just put it in your .bashrc and run it as

$ unpack file1.tbz2 file2.tZ file3.zip ...
unpack() {
  if shopt nocasematch | grep off >/dev/null 2>&1; then prevcasematch=0; fi
  shopt -s nocasematch
  while [ $# -gt 0 ]; do
    if ! [ -f $1 ]; then echo "File \"$i\" does not exist"; shift; continue; fi
    case $1 in
      *.tar.gz | *.tgz )            echo "Extracting $1 (gzipped tar archive):"; tar -xzvf $1 ;;
      *.tar.bz2 | *.tbz2 )          echo "Extracting $1 (bzipped tar archive):"; tar -xjvf $1 ;;
      *.tar.z | *.tz )              echo "Extracting $1 (compressed tar archive):"; tar -xZvf $1 ;;
      *.zip ) a=${1%%.[zZ][iI][pP]} echo "Extracting $1 (zip archive):"; mkdir $a; cd $a; unzip ../$1; cd - ;;
      *.rar ) a=${1%%.[rR][aA][rR]} echo "Extracting $1 (rar archive):"; mkdir $a; cd $a; unrar x ../$1; cd - ;;
    esac
    shift
  done
  if [ x$prevcasematch = x0 ]; then shopt -u nocasematch; fi
  }

Update: Just checked, it was neither #kde nor #fedora, it was #bash >.>

No comments: