Example 1: Deleting a directory containing a soft link to another directory.
susam@nifty:~/so$ mkdir foo bar
susam@nifty:~/so$ touch bar/a.txt
susam@nifty:~/so$ ln -s /home/susam/so/bar/ foo/baz
susam@nifty:~/so$ tree
.
├── bar
│ └── a.txt
└── foo
└── baz -> /home/susam/so/bar/
3 directories, 1 file
susam@nifty:~/so$ rm -r foo
susam@nifty:~/so$ tree
.
└── bar
└── a.txt
1 directory, 1 file
susam@nifty:~/so$
So, we see that the target of the soft-link survives.
Example 2: Deleting a soft link to a directory
susam@nifty:~/so$ ln -s /home/susam/so/bar baz
susam@nifty:~/so$ tree
.
├── bar
│ └── a.txt
└── baz -> /home/susam/so/bar
2 directories, 1 file
susam@nifty:~/so$ rm -r baz
susam@nifty:~/so$ tree
.
└── bar
└── a.txt
1 directory, 1 file
susam@nifty:~/so$
Only, the soft link is deleted. The target of the soft-link survives.
Example 3: Attempting to delete the target of a soft-link
susam@nifty:~/so$ ln -s /home/susam/so/bar baz
susam@nifty:~/so$ tree
.
├── bar
│ └── a.txt
└── baz -> /home/susam/so/bar
2 directories, 1 file
susam@nifty:~/so$ rm -r baz/
rm: cannot remove 'baz/': Not a directory
susam@nifty:~/so$ tree
.
├── bar
└── baz -> /home/susam/so/bar
2 directories, 0 files
The file in the target of the symbolic link does not survive.
The above experiments were done on a Debian GNU/Linux 9.0 (stretch) system.
X-Ref: If I rm -rf a symlink will the data the link points to get erased, too?; Deleting a folder that contains symlinks
– hakre – 2015-02-04T13:09:28.087@LordDoskias I'm personally quite happy that me and the 46 thousand people who have visited the page were able to simply read the result without experimenting themselves. And, they might have avoided an ugly disaster by having their experiments succeed and then accidentally doing
rm -rf somesymlink/
, which is destructive with the trailing/
. If 5% of the readers would've experimented, and such an experiment takes 2.5 minutes, that's 5700 minutes, or 95 hours, or a $6000 contribution to society @ $60/hr. I think we all like SO how it is, let's all ask and answer questions together :3 – Nicholas Pipitone – 2019-10-30T20:57:31.347