views:

90

answers:

3

So I created a symlink:

ln -s /location/to/link linkname

Now I want to change the location that the symlink links to. How do I do that? is there a way to do it without deleting it first?

+1  A: 

No. The symlink system call will return EEXIST if newpath already exists. You can only link from a new node in the filesystem. What's the requirement here? If you're worried about a race due to the non-atomicity of the unlink/symlink calls, then you might want to rethink the architecture a little to provide synchronization elsewhere. There have been some scary security bugs introduced by this kind of thing.

Andy Ross
what is atomic/non-atomic?
Andrew
+1  A: 

Try ln -sf new_destination linkname.

Phil
This is non-atomic, though. See my answer for details. I'm not clear whether that's the poster's concern or not.
Andy Ross
A: 

You could create the new link with a different name, then move it to replace the old link.

ln -s /location/to/link linkname

Later

ln -s /location/to/link2 newlink
mv newlink linkname

If newlink and linkname are on the same physical device the mv should be atomic.

martin clayton
what is atomic/non-atomic?
Andrew
@Andrew - http://en.wikipedia.org/wiki/Atomic_operation
martin clayton