linuxsymlinkoverwriteln

"ln -sf" does not overwrite a symlink to a directory


ln -sf

does not overwrite a given symlink to a directory.

See e.g.

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

I would have expected

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

As workaround I can explicitly unlink.

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

But I would prefer ln doing the job.

Is this possible? How?


Solution

  • 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.