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


42

这是做什么的?

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
即使您不使用-s它,也可以删除链接而不删除原始文件。硬链接会增加文件的链接数,因此当您仅删除指向该文件的链接时,该文件不会被删除。
Amok

硬链接和符号链接之间并没有太大区别。硬链接指向磁盘上的相同字节(inode)。软链接按filename指向另一个文件。
Greg Hewgill

也有手册页。即跑人ln。或者说男人,以了解该帮助系统。您也可以在线获取手册页...
Peter Cordes

Answers:


43

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

32

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

假设您有2个目录

  • 酒吧

和现有的符号链接

  • baz->酒吧

现在您想更新baz使其指向foo。如果你只是做

ln -sf foo baz

你会得到

  • baz / foo-> foo
  • baz-> bar(不变),因此
  • bar / foo-> foo

如果添加 -n

ln -sfn foo baz

你得到你想要的。

  • baz-> foo

这就是“无引用”的意思:不要解析现有链接并将新链接放在该目录中,而要对其进行更新。


2
到目前为止最有帮助的答案。
kobejohn '18

1

这是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.

-1

您可以键入“ man ln”来查找以下内容:

   -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

20
我仍然不太清楚这意味着什么
安德鲁(Andrew)2009年

-1

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

-n,--no-dereference将作为目录符号链接的目标视为正常文件


-5

-f表示,如果命令的目标是现有文件,则应将其删除并用新链接替换。(请注意,在受Unix影响的系统中,“文件”可以包括目录,链接,管道等)。

-n修改-f,表示如果您指定的目标是现有的符号链接,则不应将其删除。


3
您对的描述-n是错误的。-f本身不会替代指向目录的符号链接。将符号链接替换为目录时,-n需要将现有符号链接视为普通文件而不是目录。
布莱恩(Brian)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.