//View Tip #375
Similar Tips
» Reverse geocode with bash
» Permanent bash history
» Stream shell commands to Twitter
» Keeping your Home Directory Organized
» Incremental backup with tar

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
From dotfiles.org; original author unknown:

###   Handy Extract Program

extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xvjf $1        ;;
            *.tar.gz)    tar xvzf $1     ;;
            *.bz2)       bunzip2 $1       ;;
            *.rar)       unrar x $1     ;;
            *.gz)        gunzip $1     ;;
            *.tar)       tar xvf $1        ;;
            *.tbz2)      tar xvjf $1      ;;
            *.tgz)       tar xvzf $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
}


View Comments »




Comments 

Add your comment

Comments are currently disabled
Ian
Nice, but $1 should at least be quoted to handle spaces. Heres a modification. Not sure if the formatting will go through right.

undo () {
   ee() { # echo and execute
       echo "$@"
       $1 "$2"
   }
   for x in "$@"; do
       [[ -f $x ]] || continue
       case "$x" in
           *.tar.bz2 | *.tgz )    ee "tar xvjf" "$x"    ;;
           *.tar.gz | *.tbz2 ) ee "tar xvzf" "$x"    ;;
           *.bz2 )                ee "bunzip2" "$x"    ;;
           *.rar )                ee "unrar x" "$x"   ;;
           *.gz )                ee "gunzip" "$x"    ;;
           *.tar )                ee "tar xvf" "$x"   ;;
           *.zip )                ee "unzip" "$x"     ;;
           *.Z )                ee "uncompress" "$x" ;;
           *.7z )                ee "7z x" "$x"      ;;
       esac
   done
}
Posted 2008-11-19 06:53:56
Daenyth
I wrote this version with the purpose of having as few dependencies as possible.. bsdtar is simply the BSD version of tar.. should be simple to compile on any system; Arch Linux has it provided by the pacman package.

http://bbs.archlinux.org/viewt … 09#p511509
Posted 2009-03-07 22:06:33
Treah
Actually the tar command no longer needs the z or j to decompress bz2 or gz files. You can just type tar xvf for both :)
Posted 2010-01-05 18:59:35

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit