无法覆盖符号链接 RedHat Linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5250345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot overwrite Symbolic Link RedHat Linux
提问by Chuck Burgess
I have created a symbolic link:
我创建了一个符号链接:
sudo ln -s /some/dir new_dir
Now I want to overwrite the symbolic link to point to a new location and it will not overwrite. I have tried:
现在我想覆盖符号链接以指向新位置并且它不会覆盖。我试过了:
sudo ln -f -s /other/dir new_dir
I can always sudo rm new_dir
, but I would rather have it overwrite accordingly if possible. Any ideas?
我总是可以sudo rm new_dir
,但如果可能的话,我宁愿让它相应地覆盖。有任何想法吗?
采纳答案by ire_and_curses
ln -sfn /other/dir new_dir
works for me. The -n
doesn't dereference the destination symlink.
为我工作。在-n
不取消引用符号链接目的地。
回答by redent84
You can create it and then move it:
您可以创建它然后移动它:
sudo ln -f -s /other/dir __new_dir
sudo mv -Tf __new_dir new_dir
edit: Missing -Tf, to treat the directory as a regular file and don't prompt for overwrite.
编辑:缺少-Tf,将目录视为常规文件并且不提示覆盖。
This way you will overwrite it.
这样你就会覆盖它。
回答by Mischa
Since the Link from mikeytown2s comment is broken, i will explain why redent84s answeris better:
由于 mikeytown2s 评论中的链接已损坏,我将解释为什么redent84s 的答案更好:
While
尽管
ln -sfn newDir currentDir
does the job, it is not an atomic operation, as you can see with strace:
完成这项工作,它不是原子操作,正如您在 strace 中看到的那样:
$ strace ln -snf newDir currentDir 2>&1 | grep link
unlink("currentDir") = 0
symlink("newDir", "currentDir") = 0
This is important when you have a webserver root pointing to that symlink. It could cause errors while the symlink is deleted and created again - even in the timespan of a microsecond.
当您有一个指向该符号链接的网络服务器根目录时,这一点很重要。当符号链接被删除并再次创建时,它可能会导致错误 - 即使在微秒的时间跨度内。
To prevent errors use instead:
为了防止错误,请改用:
$ ln -s newDir tmpCurrentDir && mv -Tf tmpCurrentDir currentDir
This will create a temporary Link and after that overwrite currentDirin an atomic operation.
这将创建一个临时链接,然后在原子操作中覆盖currentDir。