I have 2 questions. First one is for the -sf options and 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 use this -s and -f options simultaneously? -s is used for creating a link file and -f is used for removing a link file. I can't understand this situation and 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?

  • 3
    -f stands for force (--force); not for remove! – Pandya Mar 26 '15 at 12:15
  • 3
    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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.