The meaning of the ln command -n parameter in Linux

reference:

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

The referenced article has made it very clear, I will add a little bit, citing the example of the original post

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

To put it simply -n is to let the existing symbolic link (ie soft link) not be escaped and parsed, which is equivalent to outputting $ var as it is instead of taking the value it is assigned.

man ln shows four ways to create links

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.

If the soft link that happens to be overwritten points to a directory, without the -n parameter, it is equivalent to the second creation method, which is to create a soft link file in a directory.

 

 

Published 27 original articles · praised 4 · visits 9695

Guess you like

Origin blog.csdn.net/yoshubom/article/details/90205440