-
Notifications
You must be signed in to change notification settings - Fork 5
Description
The command ln
behaves differently if the second argument is a link name or a directory (see https://man7.org/linux/man-pages/man1/ln.1.html).
-
The first time the command is executed (and so no symlink exist yet) the first definition of
ln
is called:ln [OPTION]... [-T] TARGET LINK_NAME
. And the program creates a symlink LINK_NAME pointing to TARGET, which from now on will be treated as a directory. -
The second time the same command is executed, as now LINK_NAME does exist and is treated as a directory, the third definition of
ln
is called:ln [OPTION]... TARGET... DIRECTORY
, which creates a symlink of TARGET inside DIRECTORY, which in our case is the "directory" LINK_NAME which has been created in the first run. -
As a consequence a recursive symlink is created inside LINK_NAME, producing the following structure
LINK_NAME/
├─ Relevant files
├─ TARGET/
│ ├─ Relevant files
│ ├─ TARGET/
│ │ ├─ Relevant files
│ │ ├─ TARGET/
│ │ │ ├─ ....
which can produce unwanted results when mixed with backup or synchronization programs.
The problematic behavior can be solved by adding the --no-target-directory
or -T
option when creating the symlinks, which force to treat LINK_NAME as a file always, which will be overwritten when combined with the -f
option.