Linux 中ln 命令-n 参数的含义

参考:

https://superuser.com/questions/645842/how-to-overwrite-a-symbolic-link-of-a-directory

参考的文章中已经说的很清楚了,我稍微补充一下,引用原帖的例子

The proper way to do this is to use the -n, --no-dereference option like so.

$ ln -snf foo2 bar

This causes ln to treat the existing symlink as a file. Otherwise, it dereferences bar to foo1, descends into foo1 and uses the original TARGET name as the LINK_NAME and that's why you end up with a symlink to foo2 being created inside the foo1 directory. The manpage on ln states the following...

-n, --no-dereference
       treat  LINK_NAME  as a normal file if it is a symbolic link to a
       directory

Below is the shell output on my Arch Linux desktop with version 8.21 of ln with and without the --no-dereference option, I got the same results you did without the --no-dereference option, but using the --no-dereference option it worked as expected.

$ mkdir foo1 foo2
$ ln -s foo1 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 bar -> foo1
$ ln -sf foo2 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 bar -> foo1
$ ls -l foo1
  total 0
  lrwxrwxrwx 1 drew users 4 Sep 17 12:51 foo2 -> foo2
$ ln -snf foo2 bar
$ ls -l bar
  lrwxrwxrwx 1 drew users 4 Sep 17 12:52 bar -> foo2

简单来说-n 就是让已存在的符号链接(即软连接)不被转义解析,就相当于$var 原样输出而不是取其被赋予的值。

man ln 显示了创建link 的四种方式

NAME

       ln - make links between files

SYNOPSIS

       ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)

       ln [OPTION]... TARGET                  (2nd form)

       ln [OPTION]... TARGET... DIRECTORY     (3rd form)

       ln [OPTION]... -t DIRECTORY TARGET...  (4th form)

DESCRIPTION

       In  the  1st  form,  create  a link to TARGET with the name LINK_NAME.  In the 2nd

       form, create a link to TARGET in the current directory.  In the 3rd and 4th forms,

       create  links to each TARGET in DIRECTORY.  Create hard links by default, symbolic

       links with --symbolic.  By default, each destination (name of new link) should not

       already  exist.  When creating hard links, each TARGET must exist.  Symbolic links

       can hold arbitrary text; if later resolved, a  relative  link  is  interpreted  in

       relation to its parent directory.

       Mandatory arguments to long options are mandatory for short options too.

如果恰巧相被覆盖的软连接指向的是一个目录的话,不加-n 参数,则相当于第二种创建方法,即在某目录下创建软连接文件。

 

 

你可能感兴趣的:(Linux 中ln 命令-n 参数的含义)