//View Tip #375
» Checksum directory recursively
» Version all unversioned files in an SVN checkout
» .. revisited
» Finding Newer Files [OR How To Create A Patch File]
Similar Tips
» Check low space» Checksum directory recursively
» Version all unversioned files in an SVN checkout
» .. revisited
» Finding Newer Files [OR How To Create A Patch File]
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
From dotfiles.org; original author unknown:
### Handy Extract Program
extract () {
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
}
Comments
Add your comment
I wrote this version with the purpose of having as few dependencies as possible.. bsdtar is simply the BSD version of tar.. should be simple to compile on any system; Arch Linux has it provided by the pacman package.
http://bbs.archlinux.org/viewt … 09#p511509
http://bbs.archlinux.org/viewt … 09#p511509
Posted 2009-03-07 22:06:33
Actually the tar command no longer needs the z or j to decompress bz2 or gz files. You can just type tar xvf for both :)
Posted 2010-01-05 18:59:35
my overly over the top version .. which lets you do
extract foo.bar | foo.* | *.bar | *
and provide extract directories with the -p option. i.e.
extract *.tgz -p ./tars
#extract - extract most common compression types
# extract <files> -p <to path> - to path is optional
# call with foo.bar or foo.* or *.bar or *
function extract () {
local args=("$@")
local rootpath=$(pwd)
local cn=0
local files=()
for f in "$@"; do
case $f in
"-p") # path option
rootpath=${args[((cn+1))]} ;
# echo 'rootpath ->' "$rootpath"
if [[ ! -d "$rootpath/" ]] ; then
echo "$rootpath does not exit, create it ? [yY/nN]" ;
read a;
if [[ $a == "Y" || $a == "y" ]] ; then
mkdir $rootpath;
else
return 0
fi;
fi
;;
*) # default
if [[ -f $f ]]; then
# add the file
files=("${files[@]}" "\"$f\"");
fi
;;
esac
((cn++))
done
# extract the files
for i in $(seq 0 $((${#files[@]} - 1))); do
# get the filenames
local f=`echo ${files[$i]} | sed -e 's/"//g'`
local fn=`echo ${files[$i]} | sed -e 's/"//g' | sed 's/.*\///'`
local local sfx=${f##*.}
local drn=$rootpath/`echo $fn | sed -e 's/.'$sfx'//g' | sed -e 's/"//g'`
local cmd=''
#drn=$rootpath/$drn
echo "Extracting -> $f "
echo "Copy -> $drn/$fn .."
# check if dir exists
[ -d "$drn" ] || mkdir "$drn"
cp -p "$f" "$drn/$fn"
# extract it
pushd "$drn" 1>/dev/null
echo "Extract -> $drn/$fn .."
case ".$sfx" in
*.t@(gz|lz|xz|)) cmd='tar xzf' ;;
*.t@(b@(2|z?(2)))) cmd='tar xjf' ;;
*.t@(a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz))))) cmd='tar xvf' ;;
*.7z) cmd='7z x' ;;
*.Z) cmd='uncompress' ;;
*.bz2) cmd='bunzip2' ;;
*.exe) cmd='cabextract' ;;
*.gz) cmd='gunzip' ;;
*.rar) cmd='unrar x' ;;
*.xz) cmd='unxz' ;;
*.zip) cmd='unzip' ;;
*) echo "'$fn' cannot be extracted via extract()" ;;
esac
#echo "cmd=$cmd"
[[ $cmd ]] && command $cmd "$fn"
echo "Remove -> $drn/$fn .."
rm -f "$fn"
popd 1>/dev/null
echo " "
done
}
extract foo.bar | foo.* | *.bar | *
and provide extract directories with the -p option. i.e.
extract *.tgz -p ./tars
#extract - extract most common compression types
# extract <files> -p <to path> - to path is optional
# call with foo.bar or foo.* or *.bar or *
function extract () {
local args=("$@")
local rootpath=$(pwd)
local cn=0
local files=()
for f in "$@"; do
case $f in
"-p") # path option
rootpath=${args[((cn+1))]} ;
# echo 'rootpath ->' "$rootpath"
if [[ ! -d "$rootpath/" ]] ; then
echo "$rootpath does not exit, create it ? [yY/nN]" ;
read a;
if [[ $a == "Y" || $a == "y" ]] ; then
mkdir $rootpath;
else
return 0
fi;
fi
;;
*) # default
if [[ -f $f ]]; then
# add the file
files=("${files[@]}" "\"$f\"");
fi
;;
esac
((cn++))
done
# extract the files
for i in $(seq 0 $((${#files[@]} - 1))); do
# get the filenames
local f=`echo ${files[$i]} | sed -e 's/"//g'`
local fn=`echo ${files[$i]} | sed -e 's/"//g' | sed 's/.*\///'`
local local sfx=${f##*.}
local drn=$rootpath/`echo $fn | sed -e 's/.'$sfx'//g' | sed -e 's/"//g'`
local cmd=''
#drn=$rootpath/$drn
echo "Extracting -> $f "
echo "Copy -> $drn/$fn .."
# check if dir exists
[ -d "$drn" ] || mkdir "$drn"
cp -p "$f" "$drn/$fn"
# extract it
pushd "$drn" 1>/dev/null
echo "Extract -> $drn/$fn .."
case ".$sfx" in
*.t@(gz|lz|xz|)) cmd='tar xzf' ;;
*.t@(b@(2|z?(2)))) cmd='tar xjf' ;;
*.t@(a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz))))) cmd='tar xvf' ;;
*.7z) cmd='7z x' ;;
*.Z) cmd='uncompress' ;;
*.bz2) cmd='bunzip2' ;;
*.exe) cmd='cabextract' ;;
*.gz) cmd='gunzip' ;;
*.rar) cmd='unrar x' ;;
*.xz) cmd='unxz' ;;
*.zip) cmd='unzip' ;;
*) echo "'$fn' cannot be extracted via extract()" ;;
esac
#echo "cmd=$cmd"
[[ $cmd ]] && command $cmd "$fn"
echo "Remove -> $drn/$fn .."
rm -f "$fn"
popd 1>/dev/null
echo " "
done
}
Posted 2010-08-11 07:47:08


undo () {
ee() { # echo and execute
echo "$@"
$1 "$2"
}
for x in "$@"; do
[[ -f $x ]] || continue
case "$x" in
*.tar.bz2 | *.tgz ) ee "tar xvjf" "$x" ;;
*.tar.gz | *.tbz2 ) ee "tar xvzf" "$x" ;;
*.bz2 ) ee "bunzip2" "$x" ;;
*.rar ) ee "unrar x" "$x" ;;
*.gz ) ee "gunzip" "$x" ;;
*.tar ) ee "tar xvf" "$x" ;;
*.zip ) ee "unzip" "$x" ;;
*.Z ) ee "uncompress" "$x" ;;
*.7z ) ee "7z x" "$x" ;;
esac
done
}