为什么要创建这样的链接:ln -nsf?

为什么要创建这样的链接:ln -nsf?

这是起什么作用的?

ln -nsf

我知道ln -s会创建一个符号链接,而不是硬链接,这意味着您可以删除它,但不会删除它所链接的内容。但是其他命令参数是什么意思?(-nf)

更新:好的……所以我想起你可以从命令行找到这些内容。这是我输入以下内容后发现的ln --help

-f, --force                 remove existing destination files
-n, --no-dereference        treat destination that is a symlink to a
                            directory as if it were a normal file

但我仍然不太清楚这到底意味着什么。我为什么要创建这样的软/符号链接?

答案1

来自 BSD 手册页:

 -f    If the target file already exists, then unlink it so that the link
           may occur.  (The -f option overrides any previous -i options.)

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

答案2

-n选项(与一起-f)强制ln更新目录的符号链接。这是什么意思?

假设你有两个目录

  • 酒吧

以及现有的符号链接

  • 巴兹 -> 酒吧

现在你想更新巴兹指向相反,如果你只是这样做

ln -sf foo baz

你会得到

  • baz/foo -> foo
  • baz -> bar (未改变),因此
  • 酒吧/ foo-> foo

如果你添加-n

ln -sfn foo baz

你得到你想要的。

  • baz -> foo

这就是“no-dereference”的含义:不解析现有链接并将新链接放在该目录内,而是仅更新它。

答案3

以下是 ln 的所有选项。您会在这里找到 -n 和 -f。

 -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.  Otherwise, 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.

答案4

-f, --force 删除现有的目标文件

-n, --no-dereference 将目录的符号链接目标视为普通文件

相关内容