ext: Give the ability to extract multiple files and wildcards

This commit is contained in:
krisdoodle45 2021-10-06 18:14:59 +02:00 committed by GitHub
parent 163738e6ad
commit 7bb07a62e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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