Last active
October 5, 2015 09:57
-
-
Save tokland/2789633 to your computer and use it in GitHub Desktop.
extractor for the most usual archive formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Apparently 7z handles more formats: http://www.linuxquestions.org/questions/linux-software-2/what-is-difference-between-usr-bin-7za-usr-bin-7z-473474/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I am correct,
7z
is now7za
.