dotfiles/.local/bin/scripts/ex

38 lines
1.1 KiB
Bash
Executable File

#!/bin/sh -e
# ex - archive extractor
# adapted and improved from the commonly circulating version
# detects whether unpacking into a subfolder is sensible
# and shows progress indications for some operations
# optdepends: rewrite(in dotfiles)
for arg do
if test -r "$arg"; then
path="$(realpath "$arg")"
name="$(basename "${path%.*}")"
(
if test "$(ls -U | wc -l)" -gt 2; then
mkdir -p "$name"
cd "$name"
fi
echo "Extracting $arg into $PWD"
case "$arg" in
(*.tar.*|*.tar) tar --extract --file "$path";;
(*.tbz2) tar xjf "$path" ;;
(*.tgz) tar xzf "$path" ;;
(*.zip|*.jar) unzip "$path" | rewrite ;;
(*.gz) gunzip "$path" ;;
(*.bz2) bunzip2 "$path" ;;
(*.rar) unrar x "$path" ;;
(*.7z|*.z01) 7z x "$path" ;;
(*.deb) ar x "$path" ;;
(*.zst) unzstd "$path" ;;
(*.Z) uncompress "$path";;
(*) echo "'$arg' cannot be extracted by ex" >&2;;
esac
test "$(ls -U | wc -l)" -lt 3 &&
mv */* . && find -empty -delete
)
else
echo "'$1' is not a readable file"
fi
done