2018-10-05 02:03:35 +00:00
|
|
|
#!/bin/sh
|
2018-08-05 04:49:13 +00:00
|
|
|
# A general, all-purpose extraction script.
|
|
|
|
#
|
|
|
|
# Default behavior: Extract archive into new directory
|
|
|
|
# Behavior with `-c` option: Extract contents into current directory
|
|
|
|
|
|
|
|
while getopts "hc" o; do case "${o}" in
|
2018-10-05 02:03:35 +00:00
|
|
|
c) extracthere="True" ;;
|
|
|
|
*) printf "Options:\\n -c: Extract archive into current directory rather than a new one.\\n" && exit ;;
|
2018-08-05 04:49:13 +00:00
|
|
|
esac done
|
|
|
|
|
2018-10-05 02:03:35 +00:00
|
|
|
if [ -z "$extracthere" ]; then
|
|
|
|
archive="$(readlink -f "$*")" &&
|
|
|
|
directory=${archive%.*} &&
|
|
|
|
mkdir -p "$directory" &&
|
|
|
|
cd "$directory" || exit
|
|
|
|
else
|
|
|
|
archive="$(readlink -f "$(echo "$*" | cut -d' ' -f2)")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ "$archive" = "" ] && printf "Give archive to extract as argument.\\n" && exit
|
2018-08-05 04:49:13 +00:00
|
|
|
|
|
|
|
if [ -f "$archive" ] ; then
|
|
|
|
case "$archive" in
|
2018-10-05 02:03:35 +00:00
|
|
|
*.tar.bz2) tar xvjf "$archive" ;;
|
|
|
|
*.tar.gz) tar xvzf "$archive" ;;
|
|
|
|
*.tar.xz) tar xvJf "$archive" ;;
|
|
|
|
*.lzma) unlzma "$archive" ;;
|
|
|
|
*.bz2) bunzip2 "$archive" ;;
|
|
|
|
*.rar) unrar x -ad "$archive" ;;
|
|
|
|
*.gz) gunzip "$archive" ;;
|
|
|
|
*.tar) tar xvf "$archive" ;;
|
|
|
|
*.tbz2) tar xvjf "$archive" ;;
|
|
|
|
*.tgz) tar xvzf "$archive" ;;
|
|
|
|
*.zip) unzip "$archive" ;;
|
|
|
|
*.Z) uncompress "$archive" ;;
|
|
|
|
*.7z) 7z x "$archive" ;;
|
|
|
|
*.xz) unxz "$archive" ;;
|
|
|
|
*.exe) cabextract "$archive" ;;
|
|
|
|
*) printf "extract: '%s' - unknown archive method\\n" "$archive" ;;
|
2018-08-05 04:49:13 +00:00
|
|
|
esac
|
2018-01-22 01:29:48 +00:00
|
|
|
else
|
2018-10-05 02:03:35 +00:00
|
|
|
printf "File \"%s\" not found.\\n" "$archive"
|
2018-08-05 04:49:13 +00:00
|
|
|
fi
|