How to remove a symbolic link to a directory?

101

11

I made a symbolic link with the following commmand:

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? (Ubuntu 8.10, bash)

Peter Smit

Posted 2009-07-20T12:40:03.283

Reputation: 7 906

4TIP: rm -r link/ deletes the content on the target – Gayan Weerakutti – 2016-09-21T09:16:33.213

Answers

122

Remove the trailing slash:

With prompt:

$ rm test5

Without prompt:

$ rm -f test5

Swanand

Posted 2009-07-20T12:40:03.283

Reputation: 1 358

It says permission denied. – 1.21 gigawatts – 2015-07-04T08:24:57.780

1Without trailing slash. So test5/ is incorrect. – pylover – 2016-08-07T12:38:35.660

21

Try rm test5
(without the training slash).

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

pelms

Posted 2009-07-20T12:40:03.283

Reputation: 8 283

14

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.

Callum

Posted 2009-07-20T12:40:03.283

Reputation: 1 526

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.

user872812

Posted 2009-07-20T12:40:03.283

Reputation: 1

1Never heard of invisible slash. What shell does this? – Kamil Maciorowski – 2018-02-14T23:30:14.380

I think it's possibly a reference to the character being hidden by linewrap+cursor? – bertieb – 2018-02-14T23:48:36.917

0

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

Posted 2009-07-20T12:40:03.283

Reputation: 161

-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

Posted 2009-07-20T12:40:03.283

Reputation: 399

2This will ask me to delete the files in the directory, that's not what I want, I want to delete only the link – Peter Smit – 2009-07-20T12:46:20.123

The asking was because interactive was defined in my .profile. Added the response to rm -r. It still doesn't work. – Peter Smit – 2009-07-20T12:50:26.620

You left the trailing '/' that's why it failed – bobby – 2009-07-20T12:52:21.330