Does rm -rf follow symbolic links?

58,295

Solution 1

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.

Solution 2

Your /home/me/msg directory will be safe if you rm -rf the directory from which you ran ls. Only the symlink itself will be removed, not the directory it points to.

The only thing I would be cautious of, would be if you called something like "rm -rf msg/" (with the trailing slash.) Do not do that because it will remove the directory that msg points to, rather than the msg symlink itself.

Solution 3

rm should remove files and directories. If the file is symbolic link, link is removed, not the target. It will not interpret a symbolic link. For example what should be the behavior when deleting 'broken links'- rm exits with 0 not with non-zero to indicate failure

Share:
58,295

Related videos on Youtube

Greg
Author by

Greg

Updated on September 18, 2022

Comments

  • Greg
    Greg over 1 year

    I have a directory like this:

    $ ls -l
    total 899166
    drwxr-xr-x 12 me scicomp       324 Jan 24 13:47 data
    -rw-r--r--  1 me scicomp     84188 Jan 24 13:47 lod-thin-1.000000-0.010000-0.030000.rda
    drwxr-xr-x  2 me scicomp       808 Jan 24 13:47 log
    lrwxrwxrwx  1 me scicomp        17 Jan 25 09:41 msg -> /home/me/msg
    

    And I want to remove it using rm -r.

    However I'm scared rm -r will follow the symlink and delete everything in that directory (which is very bad).

    I can't find anything about this in the man pages. What would be the exact behavior of running rm -rf from a directory above this one?

    • Admin
      Admin over 9 years
    • Admin
      Admin over 4 years
      @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
  • Admin
    Admin over 12 years
    "The only thing I would be cautious of, would be if you called something like "rm -rf msg/" (with the trailing slash.) Do not do that because it will remove the directory that msg points to, rather than the msg symlink itself." - I don't find this to be true. See the third example in my response below.
  • idoimaging
    idoimaging over 10 years
    I get the same result as @Susam ('rm -r symlink/' does not delete the target of symlink), which I am pleased about as it would be a very easy mistake to make.
  • Wyrmwood
    Wyrmwood over 9 years
    rm -rf baz/* will remove the contents
  • Buttle Butkus
    Buttle Butkus over 8 years
    Yes, if you do rm -rf [symlink], then the contents of the original directory will be obliterated! Be very careful.
  • Susam Pal
    Susam Pal over 6 years
    @frnknstn You are right. I see the same behaviour you mention on my latest Debian system. I don't remember on which version of Debian I performed the earlier experiments. In my earlier experiments on an older version of Debian, either a.txt must have survived in the third example or I must have made an error in my experiment. I have updated the answer with the current behaviour I observe on Debian 9 and this behaviour is consistent with what you mention.