How to extract any archive

How to extract any archive in bash? This small snippet will help you. It takes care of spaces and hyphens in the archive name (and most solutions you find on the web are wrong in this matter).

extract () {
        case "$1" in
        *.ar)        ar x -- "$1" ;;
        *.tar)       tar xf "$1" ;;
        *.tar.bz2|*.tbz2)  tar xjf "$1" ;;
        *.tar.gz|*.tgz)  tar xzf "$1" ;;
        *.tar.xz)    tar xJf "$1" ;;
        *.rar)       unrar x -- "$1" ;;
        *.zip)       unzip ./"$1" ;;
        *.7z)        7z x -- "$1" ;;
        *.gz)        gunzip -- "$1" ;;
        *.bz2)       bunzip2 -- "$1" ;;
        *)           echo "unknow package '$1'" ;;
        esac
}

Viliam is a co-founder and in a position that could be called CTO.

Posted in How To, Scripts

Leave a Reply