Extract most know archives with one command

+1
+39
-1
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()" ;;
         esac
     else
         echo "'$1' is not a valid file"
     fi
}
 
Description: 
Just pass an archive to `extract` and it will try to extract it.
Category: 

Comments

superuser'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.
https://github.com/gregwebs/ruby-extract/blob/master/e.rb#L5

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