improved extract helper tool

This commit is contained in:
Alexander Bocken 2020-10-02 15:23:37 +02:00
parent 272b158a02
commit 516a0a7b2d
Signed by: Alexander
GPG Key ID: 1D237BE83F9B05E8
3 changed files with 36 additions and 75 deletions

View File

@ -1,44 +0,0 @@
#!/bin/sh
# A general, all-purpose extraction script. Not all extraction programs here
# are installed by LARBS automatically.
#
# 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
if [ -z "$extracthere" ]; then
archive="$(readlink -f "$*")" &&
directory="$(echo "$archive" | sed 's/\.[^\/.]*$//')" &&
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
if [ -f "$archive" ] ; then
case "$archive" in
*.tar.bz2|*.tbz2) tar xvjf "$archive" ;;
*.tar.xz) tar -xf "$archive" ;;
*.tar.gz|*.tgz) tar xvzf "$archive" ;;
*.lzma) unlzma "$archive" ;;
*.bz2) bunzip2 "$archive" ;;
*.rar) unrar x -ad "$archive" ;;
*.gz) gunzip "$archive" ;;
*.tar) tar xvf "$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" ;;
esac
else
printf "File \"%s\" not found.\\n" "$archive"
fi

View File

@ -1 +1 @@
extract /home/alex/.local/bin/tools/extract

View File

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