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

UnixSymlink

Unix Problem Overview


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?

Unix Solutions


Solution 1 - Unix

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.

Solution 2 - Unix

Try ln -sf new_destination linkname.

Solution 3 - Unix

Just change the symlink target:

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

This is an instant, atomic change.

Solution 4 - Unix

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.

Solution 5 - Unix

On OSX, the man page for ln says you can do it like this

ln -shf /location/to/link link name

From the man page:

> The options are as follows:

 -F    If the target file already exists and is a directory, then remove it so that the link may occur.  The -F
       option should be used with either -f or -i options.  If none is specified, -f is implied.  The -F option is
       a no-op unless -s option is specified.

 -h    If the target_file or target_dir is a symbolic link, do not follow it.  This is most useful with the -f
       option, to replace a symlink which may point to a directory.

 -f    If the target file already exists, then unlink it so that the link may occur.  (The -f option overrides any
       previous -i options.)

 -i    Cause ln to write a prompt to standard error if the target file exists.  If the response from the standard
       input begins with the character `y' or `Y', then unlink the target file so that the link may occur.  Other-
       wise, do not attempt the link.  (The -i option overrides any previous -f options.)

 -n    Same as -h, for compatibility with other ln implementations.

 -s    Create a symbolic link.

 -v    Cause ln to be verbose, showing files as they are processed.

Solution 6 - Unix

For directories, you want to do: ln -sfT /location/to/new/target old_linkname

Solution 7 - Unix

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.

Solution 8 - Unix

Chain the commands like this:

rm currentlink && ln -s /path/to/link currentlink

The first command removes the existing one and the 2nd immediately creates it again.

Solution 9 - Unix

As others have mentioned, you basically have to delete the symlink first, either manually or by passing the -f flag to the ln utility.

Years ago, I had to make small edits to symlinks pretty frequently, so I wrote a simple readline-based utility (edln) to make this less annoying. In case anyone else finds it useful, I've put it online at https://github.com/jjlin/edln/.

edln will display the original symlink target; you can then use the arrow keys, or standard readline keystrokes (M-b, M-f, C-d, etc.) to move around and edit the target.

Solution 10 - Unix

Just googled, found no good answer and had to solve myself:

ln -f -s -T `readlink SomeLibrary | sed 's/version.old/version.new/'` SomeLibrary

Editing by definition means not recreating from scratch but changing partly. Any answer requiring to memorize a path, maybe long or with weird symbols, is definitely bad.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - Unixmartin claytonView Answer on Stackoverflow
Solution 2 - UnixPhilView Answer on Stackoverflow
Solution 3 - UnixJake WilsonView Answer on Stackoverflow
Solution 4 - Unixben_nuttallView Answer on Stackoverflow
Solution 5 - UnixjschankView Answer on Stackoverflow
Solution 6 - UnixraaView Answer on Stackoverflow
Solution 7 - UnixAndy RossView Answer on Stackoverflow
Solution 8 - UnixPeteView Answer on Stackoverflow
Solution 9 - UnixjjlinView Answer on Stackoverflow
Solution 10 - Unixuser1046885View Answer on Stackoverflow