summaryrefslogtreecommitdiff
path: root/.local/bin/extract
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2020-10-03 19:27:15 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2020-10-03 19:27:15 -0400
commite371d9edd474bcf89cf5d462eaccb8638900b390 (patch)
treeed07118f5c514a55f23a779a1507640e46fd9ed2 /.local/bin/extract
Initial commit
Diffstat (limited to '.local/bin/extract')
-rwxr-xr-x.local/bin/extract41
1 files changed, 41 insertions, 0 deletions
diff --git a/.local/bin/extract b/.local/bin/extract
new file mode 100755
index 0000000..3258d2a
--- /dev/null
+++ b/.local/bin/extract
@@ -0,0 +1,41 @@
+#!/bin/sh
+# 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
+ 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|*.tar.xz|*.tbz2) tar xvjf "$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|*.xpi) 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