Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using

ln -f -s /var/www/html/releases/build1390 app-current

to update symbolic link "app-current" with a new destination. However, this doesn't work, the link "app-current" keeps it original destination, however, I don't get any errors...

I'd rather not remove the link and recreate it, just update the target of an existing link. Is that possible?

share|improve this question

2 Answers 2

up vote 28 down vote accepted

That works for me, what is the output of strace ln -f -s /var/www/html/releases/build1390 app-current ?

Oh, since it is a directory you need to add -n for no dereference and this should solve the issue. -f is really more of a convenience since adding the -f just causes it to unlink anyways. Although I guess it would probably happen a few hundred ms faster on a normally loaded system.

For example, if arf already points to /home:

strace With -n:

strace ln -n -f -s / arf
...
symlink("/", "arf")           = -1 EEXIST (File exists)
unlink("arf")                           = 0
symlink("/", "arf")           = 0

strace Without -n:

strace ln -f -s / arf
...
write(2, "ln: "..., 4ln: )                  = 4
write(2, "`arf/': cannot overwrite director"..., 34`arf/': cannot overwrite directory) = 34
write(2, "\n"..., 1)                    = 1

So without the -n arf gets dereferenced so ln treats it as arf as if it were actually /. In your particular example, if there is no error, I think you have probably created a new symbolic link inside of /var/www/html/releases/build1390 app-current and will want to clean that up.

share|improve this answer
1  
great, it's the -n that did it. Thanks a lot! –  solsol Jun 3 '10 at 11:51
    
Thanks for explaining this :-) –  yegle Mar 4 '13 at 1:46

You can use -n or --no-dereference to prevent the target from being dereferenced if it is a symlink. You can also use -T or --no-target-directory to ensure that the target file will always be treated as a regular file.

These produce slightly different behavior, as the following example shows. Suppose src is some file, dirlink is a symlink to a directory, and dir is an actual directory.

Using -n:

  • ln -sfn src dirlink overwrites dirlink and links it to src
  • ln -sfn src dir creates link dir/src -> src

Using -T:

  • ln -sfT src dirlink overwrites dirlink and links it to src
  • ln -sfT src dir produces an error message: ln: ‘dir’: cannot overwrite directory
share|improve this answer
    
has the most correct and succinct answer for Linux users. –  ross Jul 19 '13 at 1:15
    
Is there any way to force overwriting the target directory so you won't get the error message cannot overwrite directory? –  rednaw Aug 15 '13 at 11:05
    
@rednaw You can do this by chaining a couple of commands, but ln itself does not remove directories – this would probably be a bad idea, because it would require recursively removing a possibly huge number of files. –  augurar Dec 7 '13 at 22:44

Your Answer

 
discard

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.