Skip to content

Instantly share code, notes, and snippets.

@yunginnanet
Last active August 29, 2023 02:42
Show Gist options
  • Save yunginnanet/1064acec991f94a749d8d72cb1e8d70e to your computer and use it in GitHub Desktop.
Save yunginnanet/1064acec991f94a749d8d72cb1e8d70e to your computer and use it in GitHub Desktop.
carefully move directory to diff location and then create symlink in original location to new location
#!/usr/bin/env bash
# included shebang for completeness but likely you'd wanna tack this onto a profile or bashrc, something that gets sourced by your shell
function isDir() {
if [ -d "$1" ]; then return 0; else
echo "$1 is not a directory, bail"
return 1
fi
}
function relocate() {
if !isDir "$1"; then return $?; fi
if !isDir "$2"; then return $?; fi
rsync -rauvhP ${1%"/"} ${2%"/"} || rsync -rauvhP ${1%"/"} ${2%"/"}
rsync -rauvhP ${1%"/"} ${2%"/"} || return 1
_newloc=${2%"/"}/$(basename "$1")
if !isDir "$_newloc"; then return $?; fi
mv "$1" "${1}bak"
ln -s "${_newloc}/" ${1%"/"}
if !isDir "$1"; then return $?; fi
rm -vrf "${1}bak"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment