Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There are two directories foo1 and foo2.

At first I make a directory symlink by ln -s foo1 bar. After that I want to change the symlink, so I typed ln -sf foo2 bar

But bar is still linked with foo1. Symlink foo2 is created inside of foo1.

What should I type when I want to replace foo1 by foo2?

I tested in three environments.

  1. Ubuntu12.04, ln (GNU coreutils) 8.13
  2. CentOS6.4, ln (GNU coreutils) 8.4
  3. OSX 10.7.5, ln (without version)
share|improve this question
up vote 8 down vote accepted

The proper way to do this is to use the --no-dereference option like so.

$ ln -snf foo2 bar

This causes ln to treat the existing symlink as a file. Otherwise, it dereferences bar to foo1, descends into foo1 and uses the original TARGET name as the LINK_NAME and that's why you end up with a symlink to foo2 being created inside the foo1 directory. The manpage on ln states the following...

-n, --no-dereference
       treat  LINK_NAME  as a normal file if it is a symbolic link to a

Below is the shell output on my Arch Linux desktop with version 8.21 of ln with and without the --no-dereference option, I got the same results you did without the --no-dereference option, but using the --no-dereference option it worked as expected.

$ mkdir foo1 foo2
$ ln -s foo1 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 bar -> foo1
$ ln -sf foo2 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 bar -> foo1
$ ls -l foo1
  total 0
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 foo2 -> foo2
$ ln -snf foo2 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:52 bar -> foo2
share|improve this answer
Thanks for your answer. If it is environment dependent problem, I will write my environment. Which os are you using? what outputs ln --version? – ironsand Sep 15 '13 at 23:53
I tried this on a Fedora 18 VM with version 8.17. Interestingly, I tried this on my Arch Linux desktop with version 8.21 and got the same results you did. Looks like somewhere inbetween those two versions something changed. The solution I posted worked in both instances though. – druciferre Sep 16 '13 at 0:00
FYI, I found this answer on the forums. – druciferre Sep 16 '13 at 0:10
Thanks druciferre! I even never thought that the ln command so often upgrade. – ironsand Sep 16 '13 at 23:02
I tested in osx also. ln works same as ubuntu and centos with the directory overwrite and ln -snf solve the problem. Even though ln -h is prefer option to use in osx. – ironsand Sep 17 '13 at 4:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.