112

I am using Ubuntu 8.10, Bash and I made a symbolic link with the following command:

ln -s ../test5

I want to remove it now but my rm fails:

rm -Rf test5/
rm: cannot remove `test5/`: Not a directory

rm test5/ rm: cannot remove directory test5/: Is a directory

rmdir test5/ rmdir: test5/: Not a directory

rm -r test5/ rm: cannot remove test5/: Not a directory

ls -l 0 lrwxrwxrwx 1 peter peter 8 Jul 20 15:30 test5 -> ../test5/

How can I remove my symbolic link?

Giacomo1968
  • 58,727
Peter Smit
  • 9,636

7 Answers7

137

Remove the trailing slash:

With prompt:

$ rm test5

Without prompt:

$ rm -f test5

Swanand
  • 1,508
25

Try rm test5
(without the trailing slash).

The slash indicates that 'test5' is a directory whereas it's actually a file linking to a directory.

user198350
  • 4,269
pelms
  • 9,361
16

You can run removing the trailing slash:

$ rm test5

This will remove the file (i.e. the symlink).

Alternatively you may use unlink:

$ unlink test5

Again you must omit the trailing slash since you are attempting to unlink the symlink not the directory.

bwDraco
  • 46,683
Callum
  • 1,606
2

The issue in the OP is the trailing /, so test5/ throws an error but test5 works.

I prefer to use unlink rather than rm as my intention is clearer and there is no chance of mistakenly removing the real directory instead of the link. Make sure that there is no trailing / after the directory name, e.g.:

unlink test5
isapir
  • 191
0

Sometimes if you use autocomplete to name the link that you want to delete you may not see a trailing slash but it's 'half there' and that invisible slash still gives the delete error when trying to remove that link.

So in that case type out character by character the link to be deleted as "test5" as eg. rm test5.

bertieb
  • 7,543
0

There seems to be a problem with an empty directory with two symlinks. rmdir won't work in Kubuntu 20.10. It answers "directory not empty". rm says it's a directory.

-1

I feel silly asking, but have you tried rm -r? Since it's a symbolic link it shouldn't delete the target.

Edit: Just tried it, it's correct

Edit 2: rmdir says in its first line of the man page it deletes empty directories. I would think because it's a link it had the directory bit checked on its file properties, but because rmdir doesn't suspect that being the case it spits errors. Just use rm -r

bobby
  • 477