dotfiles/.local/bin/tools/extract

48 lines
1.4 KiB
Plaintext
Raw Normal View History

2020-10-02 15:23:37 +02:00
#!/bin/bash
# A general, all-purpose extraction script. Not all extraction programs here
# are installed by LARBS automatically.
2020-07-04 14:23:27 +02:00
#
# Default behavior: Extract archive 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 archive into current directory rather than a new one.\\n" && exit ;;
esac done
2020-10-02 15:23:37 +02:00
extractfunc(){
toextract="$1"
case "$archive" in
*.tar.bz2|*.tbz2) tar xvjf "$toextract" ;;
*.tar.xz) tar -xf "$toextract" ;;
*.tar.gz|*.tgz) tar xvzf "$toextract" ;;
*.lzma) unlzma "$toextract" ;;
*.bz2) bunzip2 "$toextract" ;;
*.rar) unrar x -ad "$toextract" ;;
*.gz) gunzip "$toextract" ;;
*.tar) tar xvf "$toextract" ;;
*.zip) unzip "$toextract" ;;
*.Z) uncompress "$toextract" ;;
*.7z) 7z x "$toextract" ;;
*.xz) unxz "$toextract" ;;
*.exe) cabextract "$toextract" ;;
2020-11-07 19:09:18 +01:00
*.zst) zstd -d "$toextract";;
2020-10-02 15:23:37 +02:00
*) printf "extract: '%s' - unknown archive method\\n" "$toextract" ;;
esac
}
archives="$(readlink -f "${@##-c}" )"
while read -r archive; do
2020-07-04 14:23:27 +02:00
directory="$(echo "$archive" | sed 's/\.[^\/.]*$//')" &&
2020-10-02 15:23:37 +02:00
if [ -f "$archive" ];then
if [ -z "$extracthere" ]; then
mkdir -p $directory &&
cd "$directory" || exit
fi
extractfunc "$archive"
else
printf "File \"%s\" not found.\\n" "$archive"
fi
done < <(echo "$archives")
2020-07-04 14:23:27 +02:00
2020-10-02 15:23:37 +02:00
[ "$archives" = "" ] && printf "Give archive to extract as argument.\\n" && exit