1191

I have a symlink to an important directory. I want to get rid of that symlink, while keeping the directory behind it.

I tried rm and get back rm: cannot remove 'foo'.
I tried rmdir and got back rmdir: failed to remove 'foo': Directory not empty
I then progressed through rm -f, rm -rf and sudo rm -rf

Then I went to find my back-ups.

Is there a way to get rid of the symlink without throwing away the baby with the bathwater?

Matthew Scouten
  • 15,303
  • 9
  • 33
  • 50
  • 87
    what's wrong with asking linux-related questions? last time i checked, stack overflow was not os-specific – Jeremy Cantrell Oct 17 '08 at 14:40
  • 6
    Wouldn't this question be better suited to Super User, or a Linux-related Stack Exchange? – mwfearnley Jun 24 '15 at 14:03
  • 53
    @mwfearnley It would be, but check the date. Super User wasn't a thing yet back then. It's been ported, so it's there too. Not sure why it's still here, but it's my highest scoring question and I'm not gonna look gift karma in the mouth. – Matthew Scouten Jun 25 '15 at 18:31
  • 22
    **TIP:** `rm -r link/` deletes the content on the target – Gayan Weerakutti Sep 21 '16 at 09:16
  • 50
    `Then I went to find my back-ups` - That made me chuckle. – Frak Apr 26 '18 at 13:26
  • 3
    After going through the exact same trouble of finding my backup, I reached here to figure out the solution for future. The way you phrased the question at the end made me smile - **_with out throwing away the baby with the bathwater_** – Fr0zenFyr Aug 14 '18 at 05:31
  • 2
    `Then I went to find my back-ups` -- got me too. Ugh! – Josh M. Mar 27 '19 at 16:31

12 Answers12

1442
# this works:
rm foo
# versus this, which doesn't:
rm foo/

Basically, you need to tell it to delete a file, not delete a directory. I believe the difference between rm and rmdir exists because of differences in the way the C library treats each.

At any rate, the first should work, while the second should complain about foo being a directory.

If it doesn't work as above, then check your permissions. You need write permission to the containing directory to remove files.

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
  • 136
    ah yes. that makes sense. I never typed foo, I typed f and bash filled in a / for me. – Matthew Scouten Jan 05 '09 at 21:14
  • 6
    This does not always work. Occasionally you need to run `rm -rf folderName` (without trailing /) in order to remove the symlink. Amazon Linux behaves this way under certain circumstances, for example. – brandonscript Oct 28 '13 at 20:05
  • 22
    @r3mus: Never use `-r` with a symlink unless you want to lose everything inside it. `-f` shouldn't be needed either, except perhaps to override file permissions. – Matthew Scharley Oct 29 '13 at 00:36
  • provided you use it without the trailing slash /, I've encountered a few strange circumstances where this is required. Admittedly it's always best to make a backup of the content before attempting though, just in case ;) – brandonscript Oct 29 '13 at 03:20
  • 3
    @fxfilmxf `unlink` may not be available on some platforms where `rm` is. – Camilo Martin Aug 20 '14 at 12:21
  • @fxfilmxf (or someone else) why would you say unlink is 'better' ? – redfox05 Jul 25 '16 at 22:10
  • 1
    Update: the comments in the answer below on 'unlink' explain very well the pros/cons of unlink vs rm (http://stackoverflow.com/a/210130/339803) – redfox05 Jul 25 '16 at 22:12
  • 1
    @redfox05 I still don't get how it's better. It doesn't handle `unlink foo/` either (which would make it better - handling a common accidental error) and as noted by others, `rm foo/` will fail loudly unless you intentionally add the `-r` flag which will do... bad things. But only because you told it to. The fact there's no recursive version of `unlink` (usually - see the note about OS X's version of `unlink`) doesn't make it better, it makes it worse IMO. – Matthew Scharley Aug 08 '16 at 12:57
  • 30
    Specifically, `unlink` has no relation to the `ln` AKA 'link' operation. The name `unlink` refers to the process of unlinking/removing a file from the file system's file table so that the contents become detached from any reference to them - they are unlinked. It's a confusing name that's downright misleading when applied to links and symlinks in particular. `unlink` will work with symlinks because it works with **any file** regardless of type. – Matthew Scharley Aug 08 '16 at 13:05
  • 4
    If you're ever actually worried about doing something silly, `alias rm="rm -i"` has saved more hides than just about anything else out there I think. – Matthew Scharley Aug 08 '16 at 13:11
  • What is happening with say `rm -rf foo/` to make it delete the contents of the target? If the symbolic link is a file, shouldn't it just be treated like `rm readme.txt/`? – user2023370 Feb 20 '23 at 18:18
862

use the "unlink" command and make sure not to have the / at the end

    $ unlink mySymLink

unlink() deletes a name from the file system. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse. If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.

I think this may be problematic if I'm reading it correctly.

If the name referred to a symbolic link the link is removed.

If the name referred to a socket, fifo, or device the name for it is removed but processes that have the object open may continue to use it.

https://linux.die.net/man/2/unlink

AsukaMinato
  • 1,017
  • 12
  • 21
Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
  • 36
    @OmarAbid The "rm foo/" version scares me. This one seems a lot safer :) – Joe Phillips Mar 10 '14 at 20:48
  • I think unlink is better for removing a symbolic link and leaving the file intact. I have read that linux uses unlink for rm--but I don't think mac os x does. If you rm a symbolic link on a mac, the link and file are gone. If you unlink the symbolic link, the link is gone, but the original file is still there. – Tim May 15 '14 at 08:15
  • 9
    In Ubuntu, I tested with `~/c/a/file`, `/c/b/`, with `ln -s ~/c/a ~/c/b/`. In `~/c/b/`, `rm a/` does not work, `rm -rf a/` only empties the original `a`. `unlink a` works perfectly, and `unlink a/` does nothing. – Brady Trainor Jul 17 '14 at 00:55
  • 3
    In OS X 10.10, `unlink` is simply an alias for `rm`. Goes to show that proper `rm` discipline should be utilized when dealing with any part of your directory structure. – Doug Powers Aug 09 '15 at 13:37
  • 1
    In scripting `rm -f` will silently delete a link whether or not it exists, whereas `unlink` will complain if the link is not present. There is no `-f' option equivalent for unlink, makes the script more complex. – shonky linux user Aug 24 '15 at 01:32
  • 25
    `unlink` can delete regular files. its less featureful than `rm` and not specific to symlinks. `rm` does not delete directories without the `-r` flag either. So I recommend preferring rm, it has flags for verbose and interactive; as well as meaningful warning and error messages. – ThorSummoner Sep 15 '15 at 21:39
  • 1
    @Yvon With or without the trailing slash? – Joe Phillips Aug 21 '17 at 15:55
  • @JoePhillips Actually, I don't remember which case when I first tried. Maybe I used both, and neither worked. It is so weird, I tried again and it's working. – Yvon Aug 21 '17 at 18:08
  • @Tim that's not correct. rm works in macosx fine for deleting a symbolic link, without removing the file. – Manachi Sep 07 '17 at 22:53
  • Much safer than `rm`! – Josh M. Mar 27 '19 at 16:32
21

rm should remove the symbolic link.

skrall@skrall-desktop:~$ mkdir bar
skrall@skrall-desktop:~$ ln -s bar foo
skrall@skrall-desktop:~$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar
skrall@skrall-desktop:~$ rm foo
skrall@skrall-desktop:~$ ls -l foo
ls: cannot access foo: No such file or directory
skrall@skrall-desktop:~$ ls -l bar
total 0
skrall@skrall-desktop:~$
user16217248
  • 3,119
  • 19
  • 19
  • 37
Steve K
  • 19,408
  • 6
  • 52
  • 50
19

Use rm symlinkname but do not include a forward slash at the end (do not use: rm symlinkname/). You will then be asked if you want to remove the symlink, y to answer yes.

Brad
  • 159,648
  • 54
  • 349
  • 530
DeeEss09
  • 255
  • 2
  • 2
  • 64
    You are not wrong, but the right answer is already here and accepted (4 years ago!). Why bother? – Matthew Scouten Aug 22 '12 at 01:18
  • 2
    Google had this as the featured snippet when searching for "Remove a symlink". Maybe it liked the brevity of it, not sure how its heuristics work. Anyway, I promptly did a facepalm when I looked back at my command line :-) – Randall Feb 13 '17 at 17:25
15

Assuming it actually is a symlink,

$ rm -d symlink

It should figure it out, but since it can't we enable the latent code that was intended for another case that no longer exists but happens to do the right thing here.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • There is no `-d` argument in CentOS 6.8, Coreutil 8.4 (June 2018 release) but it exist in Xubuntu 18.04, Coreutils 8.28 (January 2018)... – karatedog Jul 22 '20 at 11:33
8

If rm cannot remove a symlink, perhaps you need to look at the permissions on the directory that contains the symlink. To remove directory entries, you need write permission on the containing directory.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
5

Assuming your setup is something like: ln -s /mnt/bar ~/foo, then you should be able to do a rm foo with no problem. If you can't, make sure you are the owner of the foo and have permission to write/execute the file. Removing foo will not touch bar, unless you do it recursively.

TJ L
  • 23,914
  • 7
  • 59
  • 77
5

I also had the same problem. So I suggest to try unlink <absolute path>.

For example unlink ~/<USER>/<SOME OTHER DIRECTORY>/foo.

U. Windl
  • 3,480
  • 26
  • 54
  • If the question contained a specific name (foo), your answer should not use something confusing like "~///foo". Also some explanation would be helpful. – U. Windl Nov 04 '20 at 08:34
3

On CentOS, just run rm linkname and it will ask to "remove symbolic link?". Type Y and Enter, the link will be gone and the directory be safe.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Yuri
  • 31
  • 1
3

I had this problem with MinGW (actually Git Bash) running on a Windows Server. None of the above suggestions seemed to work. In the end a made a copy of the directory in case then deleted the soft link in Windows Explorer then deleted the item in the Recycle Bin. It made noises like it was deleting the files but didn't. Do make a backup though!

1

you can use unlink in the folder where you have created your symlink

R. Pandey
  • 13
  • 3
0

If rm cannot remove a link, perhaps you need to look at the permissions on the directory that contains the link. To remove directory entries, you need write permission on the containing directory.

buddemat
  • 4,552
  • 14
  • 29
  • 49