1

Running the following command works as expected, creating a symlink to src from include/bb:

ln -sf ../src include/bb

However, calling the same command again will cause an additional symlink to src to be created in include/bb aka src/src.

What can I do to prevent it?


Edit:

Apparently this happens when ln is called twice, possibly the semicolon has nothing to do with it after all.

Here's a sequence of commands that will cause this to happen:

mkdir test
cd test
mkdir src
touch src/main.cpp
mkdir include
ln -sf ../src include/bb
ln -sf ../src include/bb
  • 3
    I don't think that does happen; ln doesn't even get to know about the semicolon. Can you edit in a brief script reproducing the issue you're having? – Michael Homer Mar 4 '16 at 8:55
4
ln -s ../src include/bb

either creates a link named include/bb referring to ../src (relative to its location), or it creates a file include/bb/src referring to ../src (relative to its location). The latter is the case if include/bb exists and is a directory (actually or as a link to a directory).

Thus, when you've done the command once, there is now a link include/bb that points out the directory src, sibling to include. I.e., include/bb is now a directory. Therefore, when you do the command the subsequent time, a new link named src is added to that directory.

It has nothing to do with ;.

You might have intended to use

ln -sTf ../src include/bb

instead, so as to (by -T) tell ln to treat any existing include/bb as a plain file (even though it's a link that refers to a directory), and (by -f) force that file to be replaced.

1

When you run

ln -s ../src include/bb
ln -s ../src include/bb

the first line makes a link include/bb to the directory ../src (which is what you expected).

The second one finds that include/bb already exists. If it were a regular file, or a symlink to a regular file, that would be an error (unless you gave -f as well). Instead, it is a symlink to the directory ../src, created in the previous step. When ln's target operand is a directory:

the ln utility shall create a new [...] symbolic link, for each file specified by a source_file operand, at a destination path in the existing directory named by target_dir.

So this time around you're asking to create a link to ../src inside include/bb/, which is (a link to) a directory. Those links are named by default according to their existing base names, which is src in this case. If you ran it a third time, you'd get an error because ../src/src exists now.


You can avoid this by deleting the include/bb link beforehand every time with rm include/bb. The semicolon isn't involved in this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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