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 参数,则相当于第二种创建方法,即在某目录下创建软连接文件。

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值