15

I want to upgrade tomcat server from 7.0.19 to 7.0.20.

I've already ln -s apache-tomcat-7.0.19 tomcat7 before, so now I need to link tomcat7 to new target using the following command ln -s --force apache-tomcat-7.0.21 tomcat7, but it does not worked as what I expected: it created a symbolic link tomcat7/apache-tomcat-7.0.21 instead of replace existing tomcat7 symbolic link with new target.

Example

# mkdir v1 v2
# ln -s v1 v
# ln -s v2 v
# ll v*
lrwxrwxrwx. 1 root root    2 Sep  5 16:02 v -> v1

v1:
total 0
lrwxrwxrwx. 1 root root 2 Sep  5 16:02 v2 -> v2

v2:
total 0

I checked the manual of ln, I think ln -s --force apache-tomcat-7.0.21 tomcat7 use the 3rd form instead of 1st form to explain the parameters. That's so ambiguous between the 1st and 3rd form. So how to replace symbolic link in this case?

15

You want the -T option:

$ mkdir v1
$ ln -s v1 foo
$ ls -FlA | grep foo
lrwxrwxrwx 1 phil phil     2 2011-09-05 01:58 foo -> v1/
$ mkdir v2
$ ln -s -T -f v2 foo
$ ls -FlA | grep foo
lrwxrwxrwx 1 phil phil     2 2011-09-05 01:59 foo -> v2/
  • Thanks, that's exactly what I needed. What a shame, I see this optional option in help, but I don't know what it exactly does. – LiuYan 刘研 Sep 6 '11 at 4:11
14

While replacing symbolic links to directories we should use -n option.

Example: ln -sfn /path/to/directory /target/directory

  • 1
    This should be the accepted answer. Solves the question and one-liner/ compact. – untill Feb 6 '17 at 14:31
  • The accepted answer also solves the question and is a one liner... unless I'm mistaken, it can be equally compact. I don't think it's necessary to have a separate - and space before every flag like they have. – ArtOfWarfare Dec 27 '17 at 20:08
3

Why not just

rm tomcat7
ln -s apache-tomcat-7.0.21 tomcat7

?

This will not touch the old directory apache-tomcat-7.0.19 but just delete the old link. Then ln can create the new one.

  • Thanks! I did this currently in my script, but I think it's unlike cp -f/mv -f which use single command to do overwrite operation, so I'm looking for the single command solution. – LiuYan 刘研 Sep 6 '11 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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