Removing Symbolic Links to Directories: The Trailing Slash Trap

In Linux, removing a symbolic link that points to a directory with rm -rf can produce unexpected results depending on whether a trailing / is appended to the path. Careless use of the slash may delete contents of the original directory.

Create a test directory and a symlink:

$ mkdir -p ~/workspace/project
$ echo "some content" > ~/workspace/project/file.txt
$ ln -s ~/workspace/project /tmp/proj_link

Deletion Without Trailing Slash

When you run rm -rf on the symlink without a trailing slash, only the symlink itself is removed; the oriignal directory stays intact.

$ rm -rf /tmp/proj_link
$ ls /tmp/            # symlink is gone
$ ls ~/workspace/project
file.txt             # original still exists

Deletion With Trailing Slash

If you include a trailing slash (e.g., /tmp/proj_link/), the shell interprets the command as an operation on the directory referenced by the symlink. As a result, all file inside the target directory are deleted, but the symlink remains.

Recreate the symlink for the next test:

$ ln -s ~/workspace/project /tmp/proj_link

Now execute deletion with the trailing slash:

$ rm -rf /tmp/proj_link/
$ ls /tmp/            # symlink still present
proj_link
$ ls ~/workspace/project
                     # directory is now empty

Important Takeaway

  • rm -rf <symlink> : removes the link.
  • rm -rf <symlink>/ : removes the contents of the target directory, leaving the symlink and an empty destination.

Always double-check whether you intend to operate on the link or the actual directory, especially when using shell tab-completion that might add a trailing slash.

Tags: Linux symbolic link rm Shell filesystem

Posted on Thu, 07 May 2026 07:21:52 +0000 by mahers