Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

symlink

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?

like image 275
Andrew Avatar asked Nov 13 '09 05:11

Andrew


People also ask

Does deleting a symlink delete the file it points to?

Note that removing a symbolic link does not affect the file it points to. Before removing a file, you can verify whether it is a symbolic link using the ls -l command. It will also show you the file or directory that it points to.

How do I Repoint a symlink?

To get around this it is possible to use the -f flag to 'force' the ln command to recreate the symlink without deleting it first. There is a slight niggle here in that we also need to supply the -n flag (or --no-dereference) to treat the destination that is a symlink to a directory as if it were a normal file.

What happens when you copy a symlink?

A symbolic link encountered in the tree traversal is copied instead of the file pointed to by the symbolic link. If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. This option causes cp to create special files rather than copying them as normal files.


2 Answers

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.

like image 74
martin clayton Avatar answered Sep 18 '22 20:09

martin clayton


Try ln -sf new_destination linkname.

like image 37
Phil Avatar answered Sep 18 '22 20:09

Phil