Extract most know archives with one command

extract () {
    if [ -f $1 ] ; then
      case $1 in
        *.tar.bz2)   tar xjf $1     ;;
        *.tar.gz)    tar xzf $1     ;;
        *.bz2)       bunzip2 $1     ;;
        *.rar)       unrar e $1     ;;
        *.gz)        gunzip $1      ;;
        *.tar)       tar xf $1      ;;
        *.tbz2)      tar xjf $1     ;;
        *.tgz)       tar xzf $1     ;;
        *.zip)       unzip $1       ;;
        *.Z)         uncompress $1  ;;
        *.7z)        7z x $1        ;;
        *)     echo "'$1' cannot be extracted via extract()" ;;
         echo "'$1' is not a valid file"
Just pass an archive to `extract` and it will try to extract it.


testuser's picture

This is really freaking cool, thanks for this.

pixelbeat's picture

Note tar can auto select the decompressor based on the file extention too. just:

tar -xf thefile.tar.whatever

There's the atool utility that does just that (and support more formats ...)

atool looks good. There is a Ruby script I have been using for a long time that does the same thing, except it 1) prefers to use the file command first if possible and 2) is designed for the user to add on to and change the extraction rules.

It would be nice to check if the decompression executable is present before using it ;)

You missed a compressed format. http://www.sparrowos.com/Wb/OSMain/Compress.html