I like to use relative symlinks when possible because they stay valid even when my filesystem is mounted in a non-root location, for example if I am accessing it remotely.
However, if I move a directory which contains relative symlinks, in general those links become invalid.
I am looking for a command that I can use to move a whole directory (tree), rewriting only those relative symlinks that point outside the directory so that they still point to the same files.
There is a related question on Stack Overflow, How do I move a relative symbolic link?. Also, here on Unix & Linux, find all symbolic links in a directory tree pointing outside that tree, and (referring to a much harder problem) is there some kind of symbolic links that support moving or renaming the target file?.
For example, a command called
mv-rel which behaves as follows (the other commands being their GNU coreutils versions):
$ mkdir foo $ touch bar $ ln -sr bar foo/ $ touch foo/flob $ ln -sr foo/flob foo/qux $ ls -gG foo/ total 0 lrwxrwxrwx 1 6 Dec 1 00:05 bar -> ../bar -rw-r--r-- 1 0 Dec 1 00:24 flob lrwxrwxrwx 1 4 Dec 1 00:24 qux -> flob $ mkdir baz $ mv-rel foo/ baz $ ls -gG baz/foo/ total 0 lrwxrwxrwx 1 6 Dec 1 00:05 bar -> ../../bar -rw-r--r-- 1 0 Dec 1 00:24 flob lrwxrwxrwx 1 4 Dec 1 00:24 qux -> flob
Unless I'm missing something, this also seems like it would be a useful command-line option to the 'mv' command (maybe also 'cp'). But for the purposes of this question I am looking for something that exists now, or a simple portable script that implements this functionality.