I moved a directory containing a bunch of symbolic links today, and the links all broke. I fixed the links with the following Bash oneliner (split across lines in this post for clarity):
$ for FILE in *; do
if [ -L "${FILE}" ]; then
TARGET=$(readlink "${FILE}");
rm -f "${FILE}";
ln -s "${TARGET/old-dir\//}" "${FILE}";
fi;
done
readlink
prints the value of a symbolic link and
${TARGET/old-dir\//}
creates the new link target by removing
old-dir/
from the original target.
You may also find it useful to rename simlinks so that the link filename matches the target filename:
$ for x in *; do y=$(basename $(readlink -f "${x}")); mv "${x}" "${y}"; done
This assumes that everything globbed into x
is a link. If not, you
can add an if [ -L "${x}" ]
block like I did in my
target-translation example.