21

I want to change the destination of an existing symbolic link, without removing the link or the old destination.

I have previously created a symbolic link to a directory such as follows :

$ cd /usr/lib/jvm/

$ ls -lh
drwxr-xr-x 8 uucp  143 4.0K Sep 10 20:22  jdk1.7.0_04
drwxr-xr-x 8 uucp  143 4.0K Aug 29 05:42  jdk1.7.0_07

$ sudo ln -s jdk1.7.0_04 oracle-jdk-7

$ ls -lh
drwxr-xr-x 8 uucp  143 4.0K Sep 10 20:22  jdk1.7.0_04
drwxr-xr-x 8 uucp  143 4.0K Aug 29 05:42  jdk1.7.0_07
lrwxrwxrwx 1 root root   12 May 11 11:27  oracle-jdk-7 -> jdk1.7.0_04/

Now I want to change this, to link to the other directory :

$ sudo ln --force -s jdk1.7.0_07 oracle-jdk-7

But it doesn't work without any errors :

$ ls -lh
drwxr-xr-x 8 uucp  143 4.0K Sep 10 20:36  jdk1.7.0_04
drwxr-xr-x 8 uucp  143 4.0K Aug 29 05:42  jdk1.7.0_07
lrwxrwxrwx 1 root root   12 May 11 11:27  oracle-jdk-7 -> jdk1.7.0_04/

Any help ?

1
  • Maybe this is a small detail, but it looks like the actual command you ran is sudo ln -s jdk1.7.0_04/ oracle-jdk-7. Note the slash after 04.
    – wjandrea
    Mar 31, 2018 at 20:44

1 Answer 1

31

To create a symbolic link to a directory, use the 'n' option:

ln -sfn DESTINATION_DIRECTORY LINK_NAME
  • DESTINATION_DIRECTORY is the name of the link target;
  • LINK_NAME is the name of the link.
  • The f option means to replace the existing link (ie, delete it first).

The n option is a bit complicated. I believe your command may have created a link to jdk1.7.0_07 inside the directory jdk1.7.0_04, via the link oracle-jdk-7 rather than replacing the link. If so, you should delete the link to avoid confusion.

1
  • You could also use option T, i.e. ln -sfT TARGET LINK_NAME
    – wjandrea
    Mar 31, 2018 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .