Skip to content

Instantly share code, notes, and snippets.

@nickolasburr
Last active December 1, 2017 03:15
Show Gist options
  • Save nickolasburr/c679c0ff8864660ef6fc702a23dcdbc8 to your computer and use it in GitHub Desktop.
Save nickolasburr/c679c0ff8864660ef6fc702a23dcdbc8 to your computer and use it in GitHub Desktop.
Extract various archive types.
#!/usr/bin/env bash
###
### extract: Utility for extracting various archive types.
###
shopt -s extglob
USAGE="Usage: extract FILE [FILE] ..."
if [[ $# -eq 0 ]]; then
printf '%s\n' "$USAGE"
exit 1
fi
if [[ ! -f "$1" ]]; then
printf 'Invalid file %s\n' "$1"
printf '\n%s\n' "$USAGE"
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
*.tar)
tar xf "$1"
shift
;;
*.t?(ar.)bz2)
tar xjf "$1"
shift
;;
*.t?(ar.)gz)
tar xzf "$1"
shift
;;
*.bz2)
bunzip2 "$1"
shift
;;
*.gz)
gunzip "$1"
shift
;;
*.rar)
unrar e "$1"
shift
;;
*.zip)
unzip "$1"
shift
;;
*.Z)
uncompress "$1"
shift
;;
*.7z)
7z x "$1"
shift
;;
*)
printf 'Unsupported archive type %s. Unable to extract archive contents.\n' "$1"
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment