Skip to content

Instantly share code, notes, and snippets.

@mttbernardini
Created March 9, 2021 14:00
Show Gist options
  • Save mttbernardini/b888a04a78abf11176eedf929dba070a to your computer and use it in GitHub Desktop.
Save mttbernardini/b888a04a78abf11176eedf929dba070a to your computer and use it in GitHub Desktop.
Convert absolute symlinks to relative symlinks
#!/bin/sh
for f in "$@"; do
path=$(readlink "$f")
if [ -n "$path" ]; then
cd "$(dirname "$f")"
rpath=$(realpath -s --relative-to="$PWD" "$path")
[ ! $? ] && echo "broken symlink: $f" >&2
cd - > /dev/null
if [ -n "$rpath" ] && [ "$path" != "$rpath" ]; then
echo "$f: $path -> $rpath"
ln -sfT "$rpath" "$f"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment