Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

like image 953
Marco Wahl Avatar asked Sep 07 '25 10:09

Marco Wahl


2 Answers

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.

like image 132
ceving Avatar answered Sep 09 '25 03:09

ceving


This little shell session leads to the answer.

Starting with files.

+4:25% touch f1 f2
touch f1 f2
+4:25% mkdir d1 d2
mkdir d1 d2
+4:25% ls -l
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d1
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d2
-rw-r--r-- 1 b users    0 Jun 28 16:25 f1
-rw-r--r-- 1 b users    0 Jun 28 16:25 f2
+4:25% ln -s f1 s0
ln -s f1 s0
+4:26% ls -l
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d1
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d2
-rw-r--r-- 1 b users    0 Jun 28 16:25 f1
-rw-r--r-- 1 b users    0 Jun 28 16:25 f2
lrwxrwxrwx 1 b users    2 Jun 28 16:26 s0 -> f1
+4:26% ln -s f2 s0
ln: failed to create symbolic link 's0': File exists
(1)+4:26% ln -sf f2 s0
+4:26% ls -l
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d1
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d2
-rw-r--r-- 1 b users    0 Jun 28 16:25 f1
-rw-r--r-- 1 b users    0 Jun 28 16:25 f2
lrwxrwxrwx 1 b users    2 Jun 28 16:26 s0 -> f2

Symlink directories.

+4:27% ln -sf d1 s0
+4:27% ls -l
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d1
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d2
-rw-r--r-- 1 b users    0 Jun 28 16:25 f1
-rw-r--r-- 1 b users    0 Jun 28 16:25 f2
lrwxrwxrwx 1 b users    2 Jun 28 16:27 s0 -> d1
+4:27% ln -sf d2 s0

No warning message here!

+4:27% ls -l
drwxr-xr-x 2 b users 4096 Jun 28 16:27 d1
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d2
-rw-r--r-- 1 b users    0 Jun 28 16:25 f1
-rw-r--r-- 1 b users    0 Jun 28 16:25 f2
lrwxrwxrwx 1 b users    2 Jun 28 16:27 s0 -> d1

And finally there is

+4:27% ln -sfT d2 s0
+4:27% ls -l
drwxr-xr-x 2 b users 4096 Jun 28 16:27 d1
drwxr-xr-x 2 b users 4096 Jun 28 16:25 d2
-rw-r--r-- 1 b users    0 Jun 28 16:25 f1
-rw-r--r-- 1 b users    0 Jun 28 16:25 f2
lrwxrwxrwx 1 b users    2 Jun 28 16:27 s0 -> d2
+4:28% 
like image 42
Marco Wahl Avatar answered Sep 09 '25 02:09

Marco Wahl