Created
August 27, 2014 07:28
-
-
Save ewels/12b90e5cea606324881b to your computer and use it in GitHub Desktop.
Bash function to extract any compressed file. Code snippet stolen from @robinandeer - https://github.com/robinandeer/dotfiles
This file contains hidden or 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
# One command to extract them all | |
extract () { | |
if [ $# -ne 1 ] | |
then | |
echo "Error: No file specified." | |
return 1 | |
fi | |
if [ -f $1 ] ; then | |
case $1 in | |
*.tar.bz2) tar xvjf $1 ;; | |
*.tar.gz) tar xvzf $1 ;; | |
*.bz2) bunzip2 $1 ;; | |
*.rar) unrar x $1 ;; | |
*.gz) gunzip $1 ;; | |
*.tar) tar xvf $1 ;; | |
*.tbz2) tar xvjf $1 ;; | |
*.tgz) tar xvzf $1 ;; | |
*.zip) unzip $1 ;; | |
*.Z) uncompress $1 ;; | |
*.7z) 7z x $1 ;; | |
*) echo "'$1' cannot be extracted via extract" ;; | |
esac | |
else | |
echo "'$1' is not a valid file" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice idea, but the calls to tar can be simplified. GNU tar already recognizes lots of compressed file formats automatically, no need to specify it explicitly. Just write
tar xv file.tar.bz2
, for example. The extension that's used doesn't matter, so the name could also befile.tbz2
orfile.tbz
(or anything else). This works for (at least) gzip, bz2, lzo, xz, Z formats.