#!/usr/bin/env bash
help_text="
NAME
extract - Universal archive extractor.
USAGE
extract [options] <file> [<file> ...]
OPTIONS
-h|--help
Show help text.
DESCRIPTION
Extracts various types of archive files, including tar, zip, rar, and more.
AUTHOR
mjnurse.github.io - 2026
"
help_line="Universal archive extractor"
web_desc_line="Universal archive extractor"
try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage=$(echo "Usage: ${tmp%%OPTIONS*}" | tr -d "\n" | sed "s/ */ /g")
if [[ "$1" == "" ]]; then
echo "${usage}"
echo "${try}"
exit 1
fi
while [[ "$1" != "" ]]; do
case $1 in
-h|--help)
echo "$help_text"
exit
;;
?*)
break
;;
esac
shift
done
set -euo pipefail
for file in "$@"; do
if [[ ! -f "$file" ]]; then
echo "extract: '$file' not found" >&2
continue
fi
echo "Extracting: $file"
case "$file" in
*.tar.bz2|*.tbz2) tar xjf "$file" ;;
*.tar.gz|*.tgz) tar xzf "$file" ;;
*.tar.xz|*.txz) tar xJf "$file" ;;
*.tar.zst) tar --zstd -xf "$file" ;;
*.tar) tar xf "$file" ;;
*.bz2) bunzip2 "$file" ;;
*.gz) gunzip "$file" ;;
*.xz) unxz "$file" ;;
*.zip) unzip "$file" ;;
*.7z) 7z x "$file" ;;
*.rar) unrar x "$file" ;;
*.Z) uncompress "$file" ;;
*.deb)
mkdir -p "${file%.deb}"
dpkg-deb -xv "$file" "${file%.deb}"
;;
*.rpm)
mkdir -p "${file%.rpm}"
cd "${file%.rpm}" && rpm2cpio "../$file" | cpio -idmv
cd ..
;;
*)
echo "extract: unknown format — '$file'" >&2
;;
esac
done