您可以使用不同的名称创建新链接,然后将其移动以替换旧链接。
ln -s /location/to/link linkname
稍后
ln -s /location/to/link2 newlink
mv newlink linkname
如果
newlink
和 linkname
在同一物理设备上,则 mv
应该是原子的。
尝试
ln -sf new_destination linkname
。
只需更改符号链接目标:
# ln -sfT /path/to/new/target linkname
这是一个瞬间的、原子性的改变。
如果符号链接目标是目录,则需要将
-T
标志添加到 mv
命令,否则会将新符号链接移动到旧符号链接的目标目录中。
自动将网站切换到新版本的示例:
原始设置 - 网站存储在
www1
目录中,虚拟主机指向 www
符号链接:
ln -s www1 www
浏览网站,查看旧版本。
将新网站文件放入新的
www2
目录中。
设置新网站的新符号链接:
ln -s www_new www2
将
www
符号链接移动到新网站的目录:
mv -T www_new www
浏览网站,立即查看新版本。
在 OSX 上,ln 的手册页说你可以这样做
ln -shf /location/to/link link name
来自手册页:
The options are as follows:
-F If the target file already exists and is a directory, then remove it so that the link may occur. The -F
option should be used with either -f or -i options. If none is specified, -f is implied. The -F option is
a no-op unless -s option is specified.
-h If the target_file or target_dir is a symbolic link, do not follow it. This is most useful with the -f
option, to replace a symlink which may point to a directory.
-f If the target file already exists, then unlink it so that the link may occur. (The -f option overrides any
previous -i options.)
-i Cause ln to write a prompt to standard error if the target file exists. If the response from the standard
input begins with the character `y' or `Y', then unlink the target file so that the link may occur. Other-
wise, do not attempt the link. (The -i option overrides any previous -f options.)
-n Same as -h, for compatibility with other ln implementations.
-s Create a symbolic link.
-v Cause ln to be verbose, showing files as they are processed.
对于目录,您想要执行以下操作: ln -sfT /位置/到/新/目标旧链接名称
不。如果 newpath 已存在,则
symlink
系统调用将返回 EEXIST
。您只能从文件系统中的新节点进行链接。这里有什么要求?如果您担心由于取消链接/符号链接调用的非原子性而导致的竞争,那么您可能需要稍微重新考虑架构以在其他地方提供同步。这种事情已经引入了一些可怕的安全漏洞。
正如其他人提到的,您基本上必须首先删除符号链接,无论是手动还是通过将
-f
标志传递给 ln
实用程序。
几年前,我不得不经常对符号链接进行小编辑,所以我编写了一个简单的基于读取行的实用程序(
edln
)来使这不那么烦人。如果其他人发现它有用,我已将其放在网上https://github.com/jjlin/edln/。
edln
将显示原始符号链接目标;然后,您可以使用箭头键或标准阅读行击键(M-b
、M-f
、C-d
等)来移动和编辑目标。
像这样链接命令:
rm currentlink && ln -s /path/to/link currentlink
第一个命令删除现有命令,第二个命令立即再次创建它。
刚刚用谷歌搜索,没有找到好的答案,不得不自己解决:
ln -f -s -T `readlink SomeLibrary | sed 's/version.old/version.new/'` SomeLibrary
根据定义进行编辑意味着不是从头开始重新创建,而是进行部分更改。任何需要记住路径的答案,可能很长或带有奇怪的符号,绝对是不好的。