32

How do I change where a symlink points to, without deleting and creating a new one (with ln -s "/path/to/point/to" "/path/where/symlink/is")?

When I tried doing that to Java's "Current" symlink, Java wouldn't even work (from the command line, at least, said 'Segmentation Fault') but it was back to normal when I restored the old "Current" symlink with Time Machine (but later I found out I should use /Applications/Utilities/Java Preferences.app anyway to change current java version).

mk12
  • 3,132
  • 5
  • 29
  • 34

5 Answers5

54
ln -hfs newlocation existinglink

or

ln -nfs newlocation existinglink

will change the existing link to point to newlocation

(the -n and -h are identical in operation)

From man ln

-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.)

-s Create a symbolic link

Maxxx
  • 641
  • 1
  • 5
  • 2
12

Try:

unlink /path/to/current/link
ln -s /path/to/target /path/to/symbolic/link
slhck
  • 223,558
  • 70
  • 607
  • 592
mote
  • 256
  • 1
  • 8
  • 1
    On my late 2013 MacBook Pro with Mavericks, I had to switch the two parameters: ln -s /path/to/symbolic/link /path/to/target – Marius Waldal Feb 11 '14 at 14:35
10
mkdir /path/where/newsymlink
ln -s /path/to/point/to /path/where/newsymlink/is
mv /path/where/newsymlink/is /path/where/symlink/
rmdir /path/where/newsymlink

However, the Java Preferences utility changes more than just a symbolic link; you should use that to ensure that the Java version is changed.

mark4o
  • 5,382
  • 1
  • 35
  • 30
  • wouldn't the mv command just rename it..? – mk12 Sep 06 '09 at 19:17
  • .. I tried that, it justs moves the new symlink into the folder that the old one points to. – mk12 Sep 06 '09 at 19:26
  • The first command makes the symlink that points where you want, and the second command replaces the existing pointer to the old place with the pointer to the new place. The mv is atomic so the symlink will always exist. – mark4o Sep 07 '09 at 00:00
  • .. But it still doesn't work.. it moves the new pointer into the folder that the old one points to. – mk12 Sep 07 '09 at 02:08
  • `/path/where/symlink/is` is the symlink that you are changing. Using these commands will change it to point to `/path/to/point/to`. The folder that the original symlink points to is not touched at all. – mark4o Sep 07 '09 at 07:20
  • The mv command doesn't make it replace the old one with the new one, it moves the new one into the old one (into the folder it points to). I tried several times. – mk12 Oct 16 '09 at 12:47
  • @Mk12: You're supposed to give the new one the same name as the old one. So it should overwrite it. – Sasha Chedygov Oct 19 '09 at 21:19
  • I did, but it doesn't. It moves it into the directory it points to. – mk12 Oct 25 '09 at 13:28
  • @Mk12: Sorry, you are right. I have fixed my answer to handle this, although it is now 4 commands instead of 2... :-( – mark4o Oct 26 '09 at 07:45
  • Those instructions are very hard to follow. – mk12 Oct 28 '09 at 20:07
  • It is just moving a new link over the old one. The mkdir/rmdir is just there because "mv x y" renames x to y/x instead of just y if y is a directory or a link to a directory. If mv had an option to suppress this behavior then it would be simpler. – mark4o Oct 31 '09 at 22:36
1

The ln command doesn't let you change links, only create new ones.

mk12
  • 3,132
  • 5
  • 29
  • 34
0

Have you compared the permissions on the links and on the targets before and after you change the link? You might just need to follow up with the appropriate chown and chmod commands to get it working.

Josh
  • 368
  • 1
  • 5
  • Well I don't even know how to change it so no. And I don't know what chown and chmod do. – mk12 Sep 06 '09 at 13:22
  • 2
    chown changes ownership of a file and/or directory. chmod changes permissions of a file and/or directory. These are standard in just about every unix platform. Too much detail to explain each here, so I would recommend googling each for tutorials. You can also do "man chown" or "man chmod" to read the actual manual of the command (hit q to get out of the manual). – churnd Sep 06 '09 at 13:56