#!/bin/sh # A general, all-purpose extraction script. Not all extraction programs here # are installed by LARBS automatically. # # Default behavior: Extract archives into new directory # Behavior with `-c` option: Extract contents into current directory while getopts "hc" o; do case "${o}" in c) extracthere="True" ;; *) printf "Options:\\n -c: Extract archives into current directory rather than a new one.\\n" && exit 1 ;; esac done [ -z "$extracthere" ] && archive="$(realpath "$@" 2>/dev/null)" || archive="$(realpath "${@:2}" 2>/dev/null)" [ -z "$archive" ] && printf "Give archives to extract as argument.\\n" && exit 1 while IFS= read -r f ; do if [ -f "$f" ] ; then if [ -z "$extracthere" ]; then directory="$(echo "$f" | sed 's/\.[^\/.]*$//')" && mkdir -p "$directory" && cd "$directory" || continue fi case "$f" in *.tar.bz2|*.tbz2) bsdtar -xf "$f" ;; *.tar.xz) bsdtar -xf "$f" ;; *.tar.gz|*.tgz) bsdtar -xf "$f" ;; *.tar.zst) bsdtar -xf "$f" ;; *.tar) bsdtar -xf "$f" ;; *.lzma) unlzma "$f" ;; *.bz2) bunzip2 "$f" ;; *.rar) unrar x -ad "$f" ;; *.gz) gunzip "$f" ;; *.zip) unzip "$f" ;; *.Z) uncompress "$f" ;; *.7z) 7z x "$f" ;; *.xz) unxz "$f" ;; *.exe) cabextract "$f" ;; *) printf "extract: '%s' - unknown archive method\\n" "$f" ;; esac else printf "File \"%s\" not found.\\n" "$f" fi done <<< "$archive"