您可以使用不同名称创建新链接,然后将其移动以替换旧链接。
ln -s /location/to/link linkname
稍后。ln -s /location/to/link2 newlink
mv newlink linkname
如果 newlink
和 linkname
在同一物理设备上,mv
应该是原子的。mv newlink linkname
导致 newlink
文件被删除,但没有覆盖 linkname
文件?它是否默默地执行了这个操作?这似乎非常神秘。 - Kyle Strandnewlink
和linkname
是目录,那么mv newlink linkname
会将你的newlink
移动到linkname
中。因此,这种方法并不完美。 - Urdamv -T
而不是普通的 mv
? - Zaffy尝试运行ln -sf new_destination linkname
命令。
只需更改符号链接的目标:
# ln -sfT /path/to/new/target linkname
这是一个即时、原子性的更改。
T
选项?我在我的MacOS中没有。 - Prayticbrew install coreutils
并使用 g
前缀访问 GNU 版本,例如 gln
。这样做可以避免出现微妙的差异,从而节省很多麻烦。 - Taylor D. Edmistonmv
命令中添加 -T
标志,否则它会将新符号链接移动到旧符号链接的目标目录中。www1
目录中,虚拟主机指向 www
符号链接:ln -s www1 www
浏览网站,查看旧版本。
将新的网站文件放入新的www2
目录中。
设置指向新网站的新符号链接:
ln -s www_new www2
www
符号链接移动到新网站的目录中:mv -T www_new www
打开网站,立即查看新版本。
ln -sf new_destination linkname
- ben_nuttallln -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
错误。你只能从文件系统中的新节点创建符号链接。这里有什么要求?如果你担心由于unlink/symlink调用的非原子性而导致的竞争问题,那么你可能需要重新考虑架构,在其他地方提供同步。这种事情曾经引入了一些可怕的安全漏洞。
正如其他人所提到的,你基本上必须先手动删除符号链接,或通过向ln实用程序传递-f标志来删除。
多年前,我经常需要对符号链接进行小修改,因此我编写了一个基于readline的简单实用程序(edln),以使这个过程不那么烦人。如果有人发现它有用,我已经将其放在https://github.com/jjlin/edln/上。
edln
将显示原始符号链接目标;然后您可以使用箭头键或标准readline击键(M-b、M-f、C-d等)移动和编辑目标。
read
内置的readline支持:read -e -p 'New: ' -i "$(readlink -- "$1")" lnk
。 - usretc(touch 'a b'; IFS= read -e -p 'New: ' -i ./ lnk; echo ":$lnk:"; ls "$lnk")
对我来说似乎是有效的, read -e
取消了转义;只有在按下 <TAB> 后才会出现尾随的空格,用户必须将其删除。 - usretc-r
传递给read
-- 这会带来所有的影响。然后,Tab自动补全会添加转义字符,这些字符会被read
的默认行为撤销。 - usretcrm currentlink && ln -s /path/to/link currentlink
我刚刚搜索了一下,没有找到好的答案,只能自己解决:
ln -f -s -T `readlink SomeLibrary | sed 's/version.old/version.new/'` SomeLibrary
编辑的定义是部分修改而不是从头开始重新创建。任何需要记忆路径的答案,可能会很长或带有奇怪的符号,都是不好的。