Universal Extract Script

about | blog | config | notes | github

A simple script which wraps a bunch of different extraction utilties together.

1. Install Dependencies

We're missing the package to extract *.rar files.

(zamlz/system-packages-ensure "tar")
(zamlz/system-packages-ensure "bzip2" "bunzip2")
(zamlz/system-packages-ensure "gzip" "gunzip") ;; =uncompress= is also part of this
(zamlz/system-packages-ensure "xz")
(zamlz/system-packages-ensure "unzip")
(zamlz/system-packages-ensure "p7zip" "7z")
(zamlz/system-packages-ensure "binutils" "ar")
(provide 'init-universal-extract-script)

2. Script

Make sure some argument is given

if [ ! -f "$1" ] ; then
    echo "'$1' does not exist."
    return 1
fi

Based on filetype, extract the file

case "$1" in
    *.tar.bz2)   tar xvjf "$1"   ;;
    *.tar.xz)    tar xvJf "$1"   ;;
    *.tar.gz)    tar xvzf "$1"   ;;
    *.bz2)       bunzip2 "$1"    ;;
    *.rar)       rar x "$1"      ;;
    *.gz)        gunzip "$1"     ;;
    *.tar)       tar xvf "$1"    ;;
    *.tbz2)      tar xvjf "$1"   ;;
    *.tgz)       tar xvzf "$1"   ;;
    *.zip)       unzip "$1"      ;;
    *.Z)         uncompress "$1" ;;
    *.xz)        xz -d "$1"      ;;
    *.7z)        7z x "$1"       ;;
    *.a)         ar x "$1"       ;;
    *)           echo "Unable to extract '$1'." ;;
esac

Created: 2021-11-13

Emacs 26.1 (Org mode 9.5)