Why ln -sf does not overwrite existing link to directory [closed]

人盡茶涼 提交于 2021-01-20 09:39:09

问题


According to documentation, command ln -f removes existing destination file. Does this mean that if I create a symlink, -f should remove of overwrite any existing symlink at destination?

I have a symlink, say, L, pointing to DIR1 and type ln -sf DIR2 L. But L still points to DIR1. Only after rm L this command creates a link pointing to DIR2.

With symlinks to files it behaves as expected.

What's wrong with links to directories? (bash 4.3.48 on Ubuntu 16.04.2 LTS and Windows WSL)


回答1:


When you run:

ln -sf DIR2 L

This is creating a symlink inside DIR1 cause L points to DIR1 and ln dereferences it, creating L/DIR2 -> DIR1.

The following:

rm -fr DIR1 DIR2 L
mkdir DIR1 DIR2
ln -v -s DIR1 L
ls -la L
ln -v -f -s DIR2 L
ls -la L

will output:

'L' -> 'DIR1'
lrwxrwxrwx 1 runner runner 4 Oct 21 18:13 L -> DIR1
'L/DIR2' -> 'DIR2'
lrwxrwxrwx 1 runner runner 4 Oct 21 18:13 L -> DIR1

To handle that, use the --no-dereference option as indicated in answer in this thread on superuser.com.



来源:https://stackoverflow.com/questions/52918046/why-ln-sf-does-not-overwrite-existing-link-to-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!