1178

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 with out throwing away the baby with the bathwater?

7
  • 84
    what's wrong with asking linux-related questions? last time i checked, stack overflow was not os-specific Oct 17, 2008 at 14:40
  • 6
    Wouldn't this question be better suited to Super User, or a Linux-related Stack Exchange?
    – mwfearnley
    Jun 24, 2015 at 14:03
  • 52
    @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. Jun 25, 2015 at 18:31
  • 21
    TIP: rm -r link/ deletes the content on the target Sep 21, 2016 at 9:16
  • 45
    Then I went to find my back-ups - That made me chuckle.
    – Frak
    Apr 26, 2018 at 13:26

12 Answers 12

1431
# 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.

11
  • 134
    ah yes. that makes sense. I never typed foo, I typed f<tab> and bash filled in a / for me. Jan 5, 2009 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. Oct 28, 2013 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. Oct 29, 2013 at 0:36
  • 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. Aug 8, 2016 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. Aug 8, 2016 at 13:11
858

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 which have the object open may continue to use it.

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

10
  • 36
    @OmarAbid The "rm foo/" version scares me. This one seems a lot safer :) Mar 10, 2014 at 20:48
  • 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. Jul 17, 2014 at 0: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. Aug 9, 2015 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. Aug 24, 2015 at 1:32
  • 24
    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. Sep 15, 2015 at 21:39
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:~$ 
0
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.

2
  • 64
    You are not wrong, but the right answer is already here and accepted (4 years ago!). Why bother? Aug 22, 2012 at 1: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, 2017 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.

1
  • 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, 2020 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.

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.

5

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

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

1
  • If the question contained a specific name (foo), your answer should not use something confusing like "~/<USER>/<SOME OTHER DIRECTORY>/foo". Also some explanation would be helpful.
    – U. Windl
    Nov 4, 2020 at 8: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.

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!

4
  • 3
    "It made noises like it was deleting files..." - Wow!
    – Sterex
    Apr 16, 2018 at 11:08
  • LMFAO. "It made noises like it was deleting files..."
    – Shivam Jha
    May 16, 2018 at 15:15
  • I remember floppy disks! May 17, 2018 at 16:21
  • None of the above worked for me:eddyq@eddyq-Latitude-E6440:~$ ls Desktop Downloads Dropbox-Windows Music Public Videos w Documents Dropbox examples.desktop Pictures Templates 'VmWare vmx files'
    – eddyq
    Feb 18, 2019 at 14:45
1

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

0
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.