Marco Wahl Marco Wahl - 15 days ago 7
Linux Question

How to use 'ln' to replace a symlink

ln -sf


does not override a given symlink.

+2:34% ls -ld ema*
drwxr-xr-x 22 b users 4096 Nov 25 14:33 emacs
drwxr-xr-x 22 b users 4096 Nov 25 14:29 emacs-201611181546
-rw-r--r-- 1 b users 0 Nov 25 14:34 emacs-current
+2:34% ln -fs emacs-201611181546 emacs-current
+2:34% ls -ld ema*
drwxr-xr-x 22 b users 4096 Nov 25 14:33 emacs
drwxr-xr-x 22 b users 4096 Nov 25 14:29 emacs-201611181546
lrwxrwxrwx 1 b users 18 Nov 25 14:34 emacs-current -> emacs-201611181546
+2:34% ln -fs emacs emacs-current
+2:34% ls -ld ema*
drwxr-xr-x 22 b users 4096 Nov 25 14:33 emacs
drwxr-xr-x 22 b users 4096 Nov 25 14:34 emacs-201611181546
lrwxrwxrwx 1 b users 18 Nov 25 14:34 emacs-current -> emacs-201611181546


I would have expected

lrwxrwxrwx 1 b users 18 Nov 25 14:34 emacs-current -> emacs


As workaround I can explicitly unlink.

+2:34% unlink emacs-current
+2:35% ls -ld ema*
drwxr-xr-x 22 b users 4096 Nov 25 14:33 emacs
drwxr-xr-x 22 b users 4096 Nov 25 14:34 emacs-201611181546
+2:35% ln -fs emacs emacs-current
+2:35% ls -ld ema*
drwxr-xr-x 22 b users 4096 Nov 25 14:33 emacs
drwxr-xr-x 22 b users 4096 Nov 25 14:34 emacs-201611181546
lrwxrwxrwx 1 b users 5 Nov 25 14:35 emacs-current -> emacs


But I would prefer

ln


doing the job.

Is this possible? How?

Answer

man ln

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.

You have the 3rd form, because your link is a link to a directory.