Is there a way to edit a symlink without deleting it first? [duplicate]

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?


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.


Try ln -sf new_destination linkname.


Just change the symlink target:

# ln -sfT /path/to/new/target linkname

This is an instant, atomic change.


If the symlink targets are directories, you need to add the -T flag to the mv command, otherwise it moves the new symlink in to the target directory of the old symlink.

Example of atomically switching a website to a new version:

Original setup - website is stored in www1 directory, vhost pointing at www symlink:

ln -s www1 www

Browse to website, see old version.

Put new website files in new www2 directory.

Set up new symlink to new website:

ln -s www_new www2

Move www symlink to directory of new website:

mv -T www_new www

Browse to website, see new version immediately.