Does any operating system provide a mechanism (system call — not command line program) to change the pathname referenced by a symbolic link (symlink) — other than by unlinking the old one and creating a new one?
The POSIX standard does not. Solaris 10 does not. Mac OS X 10.5 (Leopard) does not. (I’m tolerably certain neither AIX nor HP-UX does either. Judging from this list of Linux system calls, Linux does not have such a system call either.)
Is there anything that does?
(I’m expecting that the answer is "No".)
Since proving a negative is hard, let’s reorganize the question.
If you know that some (Unix-like) operating system not already listed has no system call for rewriting the value of a symlink (the string returned by readlink()) without removing the old symlink and creating a new one, please add it — or them — in an answer.
Actually, you can overwrite a symlink and thus update the pathname referenced by it:
As the OP pointed out in a comment, using the
--forceoption will makelnperform a system call tounlink()beforesymlink(). Below, the output ofstraceon my linux box proving it:The following is copied from Arto Bendiken’s answer over on unix.stackexchange.com, circa 2016.
This can indeed be done atomically with
rename(2), by first creating the new symlink under a temporary name and then cleanly overwriting the old symlink in one go. As the man page states:In the shell, you would do this with
mv -Tas follows:You can
stracethat last command to make sure it is indeed usingrename(2)under the hood:Note that in the above, both
mv -Tandstraceare Linux-specific.On FreeBSD, use
mv -halternately.This is how Capistrano has done it for years now, ever since ~2.15. See this pull request.