Skip to content

Use ln -sTf instead of ln -sf #8

@MKReyesH

Description

@MKReyesH

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

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

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions