33

I have 2 questions. The first one is for the -sf options and the second one is for the more specific usage of -f options.

By googling, I figured out the description of command ln, option -s and -f.

(copy from http://linux.about.com/od/commands/l/blcmdl1_ln.htm)

-s, --symbolic : make symbolic links instead of hard links
-f, --force : remove existing destination files

I understand these options individually. But, how could one use this -s and -f options simultaneously? -s is used for creating a link file and -f is used for removing a link file. Why use this merged option?

To know more about ln command, I made some examples.

$ touch foo     # create sample file
$ ln -s foo bar # make link to file
$ vim bar       # check how link file works: foo file opened
$ ln -f bar     # remove link file 

Everything works fine before next command

$ ln -s foo foobar
$ ln -f foo     # remove original file

By the description of -f option, this last command should not work, but it does! foo is removed.

Why is this happening?

casamia
  • 565
  • 1
  • 5
  • 7
  • 5
    `-f` stands for **force** (`--force`); not for remove! – Pandya Mar 26 '15 at 12:15
  • 4
    To add a reason to terdon's answer... One of the usage is when manually updating libraries. If you do it in two steps - first rm to remove the old link, then ln -s to create a new one - the library won't work between the operations... which becomes a great problem if the ln command needs the library to work. So with ln -sf , the old link is replaced with a new, without the link ever being broken. – Baard Kopperud Mar 26 '15 at 13:11

2 Answers2

54

First of all, to find what a command's options do, you can use man command. So, if you run man ln, you will see:

   -f, --force
          remove existing destination files

   -s, --symbolic
          make symbolic links instead of hard links

Now, the -s, as you said, is to make the link symbolic as opposed to hard. The -f, however, is not to remove the link. It is to overwrite the destination file if one exists. To illustrate:

 $ ls -l
total 0
-rw-r--r-- 1 terdon terdon 0 Mar 26 13:18 bar
-rw-r--r-- 1 terdon terdon 0 Mar 26 13:18 foo

$ ln -s foo bar  ## fails because the target exists
ln: failed to create symbolic link β€˜bar’: File exists

$ ln -sf foo bar   ## Works because bar is removed and replaced with the link
$ ls -l
total 0
lrwxrwxrwx 1 terdon terdon 3 Mar 26 13:19 bar -> foo
-rw-r--r-- 1 terdon terdon 0 Mar 26 13:18 foo
terdon
  • 234,489
  • 66
  • 447
  • 667
1
By default, each destination (name of new link) should not already exist.
  [...]   

--backup[=CONTROL]
              make a backup of each existing destination file
  [...]

 -f, --force
              remove existing destination files

You have to read carefully to understand what is meant in man ln. "Remove" is a bit misleading without the context.

With -i you get the question:

ln: replace 'q2'? y

Remove, overwrite, replace...


POSIX (man 1p ln) has:

-f
    Force existing destination pathnames to be removed to allow the link.

This is a very good addition "...to allow the link".


And info ln has:

Normally 'ln' does not replace existing files. Use the '--force' ('-f') option to replace them unconditionally, the '--interactive' ('-i') option to replace them conditionally, and the '--backup' ('-b') option to rename them.

"--backup" example: with a existing qli -> qqq:

ln -sb ttt qli

09:10 qli -> ttt
08:47 qli~ -> qqq