3

I have a directory, dir. Inside dir, there are symlinks and regular subdirectories. I want to remove dir and create a symlink for dir to point to another directory.

If I simply do rm -rf dir, I believe this should remove all the subdirectories and symlinks within dir, but is this safe to do when there are symlinks within dir?

6
  • Does this answer your question? If I rm -rf a symlink will the data the link points to get erased, too? Commented May 30, 2023 at 15:04
  • @muru I'm not sure because I can't tell if the /home3 in that question is a symlink? If it isn't, then I think it does Commented May 30, 2023 at 15:06
  • @muru The title says "if i rm -rf a SYMLINK" which seems to imply /home3 is a symlink Commented May 30, 2023 at 15:08
  • Is dir in your question a symlink? Commented May 30, 2023 at 15:09
  • @muru No, it's just a regular directory Commented May 30, 2023 at 15:13

1 Answer 1

5

Yes, it would be safe to remove the directory and its contents using rm -rf dir. That operation would not follow symbolic links and would therefore not remove files or directories that are located outside of dir. The POSIX specification for rm specifically mentions this with regard to recursive operation:

The rm utility shall not traverse directories by following symbolic links into other parts of the hierarchy, but shall remove the links themselves.

If dir itself is a symbolic link (you say it's a directory, so this may not apply to your situation), then that symbolic link will be removed, but not the thing that it points to. Note that in this case, rm -rf dir/ (with the final slash added) would dereference the symbolic link and delete the directory that the link points to, while the symbolic link would remain.

1
  • 1
    IIRC whether rm -rf dir/ where dir is a symlink, removes that symlink (and/or the target and its contents) is system / rm implementation dependant. Commented May 30, 2023 at 16:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.