extract (1272B)
1 #!/usr/bin/env sh 2 # A general, all-purpose extraction script. 3 # 4 # Default behavior: Extract archive into new directory 5 # Behavior with `-c` option: Extract contents into current directory 6 7 while getopts "hc" o; do case "${o}" in 8 c) extracthere="True" ;; 9 *) printf "Options:\\n -c: Extract archive into current directory rather than a new one.\\n" && exit ;; 10 esac done 11 12 if [ -z "$extracthere" ]; then 13 archive="$(readlink -f "$*")" && 14 directory="$(echo "$archive" | sed 's/\.[^\/.]*$//')" && 15 mkdir -p "$directory" && 16 cd "$directory" || exit 17 else 18 archive="$(readlink -f "$(echo "$*" | cut -d' ' -f2)")" 19 fi 20 21 [ "$archive" = "" ] && printf "Give archive to extract as argument.\\n" && exit 22 23 if [ -f "$archive" ] ; then 24 case "$archive" in 25 *.tar.bz2|*.tar.xz|*.tbz2) tar xvjf "$archive" ;; 26 *.tar.gz|*.tgz) tar xvzf "$archive" ;; 27 *.lzma) unlzma "$archive" ;; 28 *.bz2) bunzip2 "$archive" ;; 29 *.rar) unrar x -ad "$archive" ;; 30 *.gz) gunzip "$archive" ;; 31 *.tar) tar xvf "$archive" ;; 32 *.zip) unzip "$archive" ;; 33 *.Z) uncompress "$archive" ;; 34 *.7z) 7z x "$archive" ;; 35 *.xz) unxz "$archive" ;; 36 *.exe) cabextract "$archive" ;; 37 *) printf "extract: '%s' - unknown archive method\\n" "$archive" ;; 38 esac 39 else 40 printf "File \"%s\" not found.\\n" "$archive" 41 fi