Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question
    
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. –  Iain 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. –  Iain 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. –  Iain Mar 16 '13 at 21:42

2 Answers 2

up vote 5 down vote accepted

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
share|improve this answer
    
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 8: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 8:37
    
OK but I also try this with -n , and this not remove the old link . –  Eytan Mar 18 '13 at 9:31

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

mv link new_link
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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