Created
October 6, 2014 07:02
-
-
Save koenhendriks/6840f542d9e7b40b70ec to your computer and use it in GitHub Desktop.
Extract Function Linux Bash
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
# Extract archives - use: extract <file> | |
# Based on http://dotfiles.org/~pseup/.bashrc | |
function extract() { | |
if [ -f "$1" ] ; then | |
local filename=$(basename "$1") | |
local foldername="${filename%%.*}" | |
local fullpath=`perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1"` | |
local didfolderexist=false | |
if [ -d "$foldername" ]; then | |
didfolderexist=true | |
read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1 | |
echo | |
if [[ $REPLY =~ ^[Nn]$ ]]; then | |
return | |
fi | |
fi | |
mkdir -p "$foldername" && cd "$foldername" | |
case $1 in | |
*.tar.bz2) tar xjf "$fullpath" ;; | |
*.tar.gz) tar xzf "$fullpath" ;; | |
*.tar.xz) tar Jxvf "$fullpath" ;; | |
*.tar.Z) tar xzf "$fullpath" ;; | |
*.tar) tar xf "$fullpath" ;; | |
*.taz) tar xzf "$fullpath" ;; | |
*.tb2) tar xjf "$fullpath" ;; | |
*.tbz) tar xjf "$fullpath" ;; | |
*.tbz2) tar xjf "$fullpath" ;; | |
*.tgz) tar xzf "$fullpath" ;; | |
*.txz) tar Jxvf "$fullpath" ;; | |
*.zip) unzip "$fullpath" ;; | |
*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;; | |
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