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

And*_*rew 63 unix symlink

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

ln -s /location/to/link linkname
Run Code Online (Sandbox Code Playgroud)

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

mar*_*ton 40

您可以使用其他名称创建新链接,然后移动它以替换旧链接.

ln -s /location/to/link linkname
Run Code Online (Sandbox Code Playgroud)

后来

ln -s /location/to/link2 newlink
mv newlink linkname
Run Code Online (Sandbox Code Playgroud)

如果newlinklinkname在同一物理设备上,则mv应该是原子的.

  • 如果它们是目录,`mv newlink linkname`会将你的`newlink`移动到`linkname`.所以这种方法并非100%完美. (3认同)

Phi*_*hil 26

试试ln -sf new_destination linkname.

  • 这也不适用于指向目录的符号链接.它只会在旧目标目录中创建一个新的符号链接. (11认同)
  • 使用-n( - no-dereference)开关来解决此问题. (11认同)
  • 但这是非原子的。详情请参阅我的回答。我不清楚这是否是发帖者所关心的。 (2认同)

ben*_*all 17

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

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

原始设置 - 网站存储在www1目录中,vhost指向www符号链接:

ln -s www1 www
Run Code Online (Sandbox Code Playgroud)

浏览网站,查看旧版本.

将新网站文件放在新www2目录中.

为新网站设置新的符号链接:

ln -s www_new www2
Run Code Online (Sandbox Code Playgroud)

www符号链接移动到新网站的目录:

mv -T www_new www
Run Code Online (Sandbox Code Playgroud)

浏览网站,立即查看新版本.


Jak*_*son 15

只需更改符号链接目标:

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

这是一个瞬间的原子变化.


小智 7

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


jsc*_*ank 6

在OSX上,ln的手册页说你可以这样做

ln -shf /location/to/link link name
Run Code Online (Sandbox Code Playgroud)

从手册页:

The options are as follows:
Run Code Online (Sandbox Code Playgroud)
 -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.
Run Code Online (Sandbox Code Playgroud)


And*_*oss 5

否.如果newpath已存在,symlink系统调用将返回EEXIST.您只能从文件系统中的新节点进行链接.这里有什么要求?如果由于unlink/symlink调用的非原子性而担心竞争,那么您可能需要重新考虑该体系结构以在其他地方提供同步.这种事情引入了一些可怕的安全漏洞.