Why use 'ln -sf' in Linux?

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

Tags:

Shell

Ln