Trick: The Simplest Program to Extract Archives

From SHellium Wiki
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

Ever wondered what those gazillion commands to extract various types of archives, .7z, .tar.gz, .tar.bz2, etc are ? Here is a simple trick you can do simplify the whole process.

extract() {
    if [ -f "$1" ] ; then
        case "$1" in
            *.tar.bz2) tar xjf "$1" ;;
            *.tar.gz) tar xzf "$1" ;;
            *.tar.Z) tar xzf "$1" ;;
            *.bz2) bunzip2 "$1" ;;
            *.rar) rar e "$1" ;;
            *.gz) gunzip "$1" ;;
            *.jar) 7z x "$1" ;;
            *.tar) tar xf "$1" ;;
            *.tbz2) tar xjf "$1" ;;
            *.tgz) tar xzf "$1" ;;
            *.zip) 7z x "$1" ;;
            *.Z) uncompress "$1" ;;
            *.7z) 7z x "$1" ;;
            *) echo "'$1' cannot be extracted" ;;
        esac
    else
        echo "'$1' is not a file"
    fi
}

Copy the above text to the end of your .bashrc file. Now to extract a file named "somefile.foo.boo" all you need to do is run

extract somefile.foo.boo

and the file gets extracted !!!!

Note: For changes in .bashrc to take effect your bash session needs to be restarted. Since you often don't want to do that, you need to run this command instead for changes in .bashrc to take effect instantly:

source ~/.bashrc
Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox