Why create a link like this: ln -nsf?

From the BSD man page:

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

the -n option (together with -f) forces ln to update a symbolic link to a directory. what does that mean?

suppose you have 2 directories

  • foo
  • bar

and an existing symbolic link

  • baz -> bar

now you want to update baz to point to foo instead. if you just do

ln -sf foo baz

you would get

  • baz/foo -> foo
  • baz -> bar (unchanged), and thus
  • bar/foo -> foo

if you add -n

ln -sfn foo baz

you get what you want.

  • baz -> foo

that is what 'no-dereference' means: do not resolve an existing link and place the new link inside that directory, but rather just update it.


Here are all the options to ln. You'll find -n and -f in here.

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