Skip to content

Instantly share code, notes, and snippets.

@tokland
Last active October 5, 2015 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokland/2789633 to your computer and use it in GitHub Desktop.
Save tokland/2789633 to your computer and use it in GitHub Desktop.
extractor for the most usual archive formats
#!/bin/bash
# all-in-one extractor for the most usual archive formats. Requires Bash >= 4
set -e -u -o pipefail
declare -A COMMANDS=(
[z]="compress -d"
[gz]="gunzip"
[bz2]="bunzip2"
[zip]="unzip -qo"
[rar]="unrar x"
[tar]="tar xf"
[tgz]="tar xzf"
[7z]="7z x"
)
if test $# -eq 0; then
echo -e "Usage: extract FILE [FILE2 ...]\n"
echo "Supported extensions: ${!COMMANDS[@]}"
exit 2
fi
for FILE in "$@"; do
EXTENSION=$(echo "$FILE" | awk -F . '{print $NF}' | tr '[A-Z]' '[a-z]')
COMMAND=${COMMANDS[$EXTENSION]:-}
if test "$COMMAND"; then
$COMMAND "$FILE"
else
echo "[$FILE] unknown extension: $EXTENSION" >&2
fi
done
@santosh
Copy link

santosh commented Feb 28, 2013

If I am correct, 7z is now 7za.

@tokland
Copy link
Author

tokland commented Apr 25, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment