Skip to content

Instantly share code, notes, and snippets.

@ewels
Created August 27, 2014 07:28
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 ewels/12b90e5cea606324881b to your computer and use it in GitHub Desktop.
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
# 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
}
@marcelm
Copy link

marcelm commented Aug 29, 2014

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 be file.tbz2 or file.tbz (or anything else). This works for (at least) gzip, bz2, lzo, xz, Z formats.

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