有没有办法编辑一个符号链接,而不是先删除它?

所以我创build了一个符号链接:

ln -s /location/to/link linkname 

现在我想改变符号链接的位置。 我怎么做? 有没有办法做到这一点,而不是先删除它?

您可以使用不同的名称创build新链接,然后移动它以replace旧的链接。

 ln -s /location/to/link linkname 

后来

 ln -s /location/to/link2 newlink mv newlink linkname 

如果newlinklinkname在同一个物理设备上,那么mv应该是primefaces的。

尝试ln -sf new_destination linkname

如果符号链接目标是目录,则需要将-T标志添加到mv命令,否则将新的符号链接移动到旧符号链接的目标目录中。

自动将网站切换到新版本的示例:

原始设置 – 网站存储在www1目录,虚拟主机指向www符号链接:

 ln -s www1 www 

浏览网站,查看旧版本。

把新的网站文件放在新的www2目录中。

build立新的符号链接到新的网站:

 ln -s www_new www2 

www symlink移动到新网站的目录:

 mv -T www_new www 

浏览网站,立即看到新版本。

对于目录,您需要执行以下操作:ln -sfT / location / to / new / target old_linkname

如果新path已经存在, symlink系统调用将返回EEXIST 。 您只能链接文件系统中的新节点。 这里有什么要求? 如果由于unlink / symlink调用的非primefaces性而担心竞争,那么您可能需要重新考虑体系结构,以便在别处提供同步。 这种事情引发了一些可怕的安全漏洞。

在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. 

链接这样的命令:

 rm currentlink && ln -s /path/to/link currentlink 

第一个命令删除现有的和第二个立即再次创build它。

正如其他人所提到的,你首先必须手动删除符号链接,或者通过将-f标志传递给ln工具。

几年前,我不得不经常对符号链接进行小的编辑,所以我写了一个简单的基于readline的实用程序( edln ),使其不那么烦人。 如果其他人发现它有用,我已经把它在网上https://github.com/jjlin/edln/

edln将显示原始的符号链接目标; 然后可以使用箭头键或标准readline击键( MbMfCd等)来移动和编辑目标。

只需更改符号链接目标:

# ln -sfT /path/to/new/target linkname

这是一个瞬间,primefaces的变化。

只是search了一下,发现没有好的答案,必须解决自己:

 ln -f -s -T `readlink SomeLibrary | sed 's/version.old/version.new/'` SomeLibrary 

按照定义进行编辑意味着不要从头重新创build,而要部分地改变。 任何需要记住path的答案,可能很长,或者带有奇怪的符号,肯定是不好的。