Skip to content

Instantly share code, notes, and snippets.

@okhlybov
Created April 10, 2020 12:39
Show Gist options
  • Save okhlybov/d6884e282dfa0dc59401d0bce08f6e69 to your computer and use it in GitHub Desktop.
Save okhlybov/d6884e282dfa0dc59401d0bce08f6e69 to your computer and use it in GitHub Desktop.
Bash tarball extractor replacing symlinks with real copies for MSYS(2) environments and alike.
# Bash tarball extractor function which replaces symlinks with real copies.
# Useful for platforms which do not handle symlinks well, such as MSYS(2).
untar() {
case "$1" in
*.tar)
u=
;;
*.tar.gz|*.tgz)
u=z
;;
*.tar.bz|*.tar.bz2|*.tbz2|*.tbz)
u=j
;;
*.tar.xz|*.txz)
u=J
;;
*)
exit -1
;;
esac
tar x${u}f $1 2> /dev/null
ifs=$IFS
IFS=$'\n'
for s in `tar tv${u}f $1 | grep '^l.*->' -`; do
IFS=' ' read -r -a f <<< "$s"
dst="${f[8]}"
src=`dirname "$dst"`/"${f[10]}"
cp -r "$src" "$dst"
done
IFS=$ifs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment