33

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,342

5 Answers5

58
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
  • 681
12

Try:

unlink /path/to/current/link
ln -s /path/to/target /path/to/symbolic/link
slhck
  • 235,242
mote
  • 256
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,552
1

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

mk12
  • 3,342
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
  • 378