| yorickthepoor@gmail.com 2006-01-13, 10:40 pm |
| I recently wanted to find all symlinks which pointed to an outdated
mount point and change them. I found a couple of scripts on the net
to search and replace strings in symbolic links, but they seemed to not
be immune to special character, so I came up with the following bash
script. It respects double quotes, single quotes, other shell
meta-characters, and newlines, both in the link name and the link
target. It seems hacky so I thought I would post it, hoping to elicit
better ways to accomplish this task. Feedback greatly appreciated.
[begin script]
#!/usr/bin/env bash
if test -z "$1"; then
echo "usage: $(basename $0) <searchpath>"
echo ""
echo "<searchpath> path to look for symbolic links"
exit 1
fi
find "$1" -type l -lname "/the/wrong/directory/*" \
-printf '%l\000' -printf '%p\000' \
| sed -e 's@/the/wrong/directory@/the/right/directory@g' \
| xargs -0 -l2 bash -s -c 'ln -nfs "$0" "$1"'
[end script]
--
Poor Yorick
|