0

I've done a quick search but I can't find anything specific about this scenario:

Let's say I have a directory (let's ignore the path) and call it A. Now, I create a symlink to directory A called S. We should now have:

S -> A

If I then have another directory, let's say B and do something along the lines of:

cp -r B/ A/

will the symlink persist, or would this break it? Let's say instead of using the bash command, I use the python distutils.dir_utils.copytree(B, A), would the symlink S still point to A? Is A still A or has it become B?

3 Answers 3

1

You can test this from the command line fairly easily:

# set up test
mkdir A
mkdir B
echo "this is A" > A/test.txt
echo "this is B" > B/test.txt
echo "this is A" > A/old.txt
echo "this is B" > B/new.txt

# contents of A change on copy
ln -s A S
cat S/test.txt
cp -r B/ A/
cat S/test.txt
cat A/test.txt

# S still points to A
ls -l S/
ls -l A/
ls -l B/

As you will be able to see files from B that overwrite files from A will have an impact on both A and S. However as you are copying the files from A with names unique between A and B will remain because S is still pointing to A but A has changed.

Symbolic links point to a path. For example if you remove A and leave S, S still refers to A which no longer exists:

rm -rf A/
ls -l S/

If you were to now create a text file with the called A, S would point to this text file as it is still pointing to the path A. As Kusalananda and G-Man say nothing you do to A will affect S. Or more explicitly nothing you do to A will affect the fact that S points to A.

echo "new text file" > A
cat S
3
  • I see, thank you for the detailed response.
    – Siewiei
    Aug 23, 2019 at 19:28
  • 2
    "S will no longer be a directory". In fact, S was never a directory. It was, and remains, a symbolic link. Nothing that you do to the path pointed to by the symbolic link S affects the symbolic link itself. G-Man is 100% correct in this. You have the wrong concept of what a symbolic link is.
    – Kusalananda
    Aug 23, 2019 at 19:28
  • @Kusalananda you are correct, I worded that in a very misleading way. I edited the answer, although I'm sure it could still be edited further to be more explicit and correct. You are also right that G-Man is correct, I do however believe his answer could also be misleading.
    – jTables
    Aug 23, 2019 at 19:52
1

The symbolic link S points to A by means of the path to A. Copying files into A (which is what your command is doing) will not affect this link.

If you, for whatever reason, renamed or removed A, then the symbolic link S would be broken and unusable. If you then created something else, possibly a new directory, called A in the same location as the old A, then S would be a symbolic link to that thing instead.

Note that the symbolic link S is totally separate from the directory A. S in itself does not care whether A exists or not, or whether it is a file, a directory or something else. Anything you do to A will not affect S.

The act of dereferencing the symbolic link S (i.e. using it, for example by doing ls S) will have different outcomes depending on what A is (if it exists, if it's a directory, etc.)

1
  • Thank you for the brief answer. It also answered my question but I decide to go with jTables' answer as it was more detailed.
    – Siewiei
    Aug 23, 2019 at 19:31
0

Imagine that you had a file called S.txt (in some separate directory) that said

The information that you’re looking for is in directory A.

It wouldn’t be affected by anything you did to directory A.

Symbolic links are very much like this.  The symbolic link won’t be affected by anything you do to its target.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .