1

according to ln manual page:

   man ln


   -f, --force
          remove existing destination files

so as I understand if I want to re-create new link to some destination directory/file

I can simply do

    ln -s -f some_directory new_link

but this is not what I got from my linux/solaris machines

for example

  [u@h w]# mkdir dir1
  [u@h w]# ln -s dir1 link
  [u@h w]# ls -l


  drwxr-xr-x 2 root root 4096 Mar 16 20:26 dir1
  lrwxrwxrwx 1 root root    4 Mar 16 20:27 link -> dir1




   [u@h w]# ln -s -f dir1 new_link

now I accepted to see that new_link is now pointed to dir1 while "link" isn’t exists but link still pointed to dir1 also?

how to recreate new link and on the same time remove the old link in one command ?

( I not want to use rm command )

  [u@h w]# ls -l
  drwxr-xr-x 2 root root 4096 Mar 16 20:26 dir1
  lrwxrwxrwx 1 root root    4 Mar 16 20:27 link -> dir1
  lrwxrwxrwx 1 root root    4 Mar 16 20:28 new_link -> dir1

Other different example

  # 
  # mkdir dir
  # 
  # ln -s dir link
  # 
  # mv dir dir_new
  # ls -l
  total 32
    drwxr-xr-x   2 root     root         117 Mar 16 21:44 dir_new
    lrwxrwxrwx   1 root     root           3 Mar 16 21:44 link -> dir
  # ln -s -f  dir_new link
  ln: cannot create link: File exists

is it possible to update the current link to point on new directory/file

( without to get File exists ? ) ,

or force to create the link again on different directory ?

Eytan
  • 611
  • 6
  • 13
  • 27
  • Why are you placing silly restrictions on how to do this ? Why can't you use rm ? This is about system administration, not [code golf](http://codegolf.stackexchange.com/). – user9517 Mar 16 '13 at 21:22
  • why to use "silly" , It is very offensive , I not want to use rm because I have external requirement and because safety issues – Eytan Mar 16 '13 at 21:28
  • Using rm on a soft link isn't in any way dangerous, it simply removes the link to the target. – user9517 Mar 16 '13 at 21:32
  • ok I see but I cant use rm because external requirement about code review , so if you have some other idea I will happy to ear -:) – Eytan Mar 16 '13 at 21:35
  • 1
    Then you need to speak to the people who are placing this silly restriction upon you because quite frankly this isn't a hoop you need to jump through. – user9517 Mar 16 '13 at 21:42
  • @Lain thanx I will do so , good night – Eytan Mar 16 '13 at 21:45

2 Answers2

7

The -f option allows you to overwrite the target file, i.e. the file with the name you're creating the link with, with a new link. It doesn't check for other links in the same directory.

Example:

$ touch file1
$ touch file2
$ ln -s file1 link1
$ ls -l
total 8
-rw-r--r--  1 jenny  staff    0 Mar 16 21:13 file1
-rw-r--r--  1 jenny  staff    0 Mar 16 21:13 file2
lrwxr-xr-x  1 jenny  staff    5 Mar 16 21:13 link1 -> file1
$ ln -s file2 link1
ln: link1: File exists
$ ln -s -f file2 link1
$ ls -l
total 8
-rw-r--r--  1 jenny  staff  0 Mar 16 21:13 file1
-rw-r--r--  1 jenny  staff  0 Mar 16 21:13 file2
lrwxr-xr-x  1 jenny  staff  5 Mar 16 21:14 link1 -> file2

If you just want to move the name of the link, so that instead of being called link1 it should be called newlink, just move it:

$ mv link1 newlink
$ ls -l
total 8
-rw-r--r--  1 jenny  staff  0 Mar 16 21:13 file1
-rw-r--r--  1 jenny  staff  0 Mar 16 21:13 file2
lrwxr-xr-x  1 jenny  staff  5 Mar 16 21:14 newlink -> file2

If you're working with directories instead of files, you need to add the option -n as well as -f:

$ ls -l
total 16
drwxrwxr-x 2 jenny jenny 4096 Mar 18 10:37 dir1
drwxrwxr-x 2 jenny jenny 4096 Mar 18 10:37 dir2
$ ln -s dir1 link1
$ ls -l
total 20
drwxrwxr-x 2 jenny jenny 4096 Mar 18 10:37 dir1
drwxrwxr-x 2 jenny jenny 4096 Mar 18 10:37 dir2
lrwxrwxrwx 1 jenny jenny    4 Mar 18 10:37 link1 -> dir1
$ ln -s -n -f dir2 link1
$ ls -l
total 20
drwxrwxr-x 2 jenny jenny 4096 Mar 18 10:37 dir1
drwxrwxr-x 2 jenny jenny 4096 Mar 18 10:37 dir2
lrwxrwxrwx 1 jenny jenny    4 Mar 18 10:38 link1 -> dir2
Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • please see my update quastion - the last one ( is it possible to update the current link to point on new directory/file ) , this is diff scenario , what I want is to force the curent link to update on new directory name but link name will stay , is it possible ? – Eytan Mar 16 '13 at 20:42
  • That is exactly what -f is for. But when the links are directories, the OS will assume that you mean to create a link within the directory, so you need to add the option `-h` for it to work. – Jenny D Mar 17 '13 at 08:06
  • Jenny ( I looked on man ln and I not see the option -h ) , did you mean to perfrom the command like this : ln -s -f -n dir new_link ? – Eytan Mar 17 '13 at 18:28
  • Apparently I was looking at a box with a different OS from yours. Yes, on linux it would be -n instead of -h. – Jenny D Mar 18 '13 at 08:37
  • OK but I also try this with -n , and this not remove the old link . – Eytan Mar 18 '13 at 09:31
  • I have no idea what could cause that. I've tried this on linux, solaris and macosx, and it works perfectly. I've updated my answer with that information as well – Jenny D Mar 18 '13 at 09:40
  • ok - choose your answer – Eytan Mar 23 '13 at 22:42
  • `ln -s -f` will `unlink` the old directory entry (the old link) before creating the new one, so there will be a point in the process (in time) where the "link" does not exist in the directory. To avoid this, it is necessary to first create the new link in the same directory and then use `mv` which in turn uses `rename`(2) to replace the old link with the new one. - as suggested in the MadHatter's answer. – Zrin Nov 03 '16 at 13:25
4

mv is atomic, and will do what you want, as far as I can tell:

mv link new_link
MadHatter
  • 79,770
  • 20
  • 184
  • 232
  • `mv` internally uses `rename`(2) which is atomic only if both old file and new file (link) is under the same mount point. If both "link" and "new_link" are in the same directory, then it's the way to do it. – Zrin Nov 03 '16 at 13:08
  • actually _under the same **nearest** mount point and (thus) on the same file system_ - as one file system could be mounted in different places and in that case atomicity wouldn't be guaranteed. – Zrin Nov 03 '16 at 13:19