1197

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?

6
  • 27
    TIP: rm -r link/ deletes the content on the target Commented Sep 21, 2016 at 9:16
  • 4
    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
    Commented Aug 14, 2018 at 5:31
  • Related: Safely remove a symlink or hardlink Commented Sep 3, 2023 at 22:28
  • From Remove Symlink to home directory: "If you have just created a symlink, you can simply rm it. Without the -r and -f it will not delete directories" Commented Sep 3, 2023 at 22:52
  • @Peter Mortensen This was a genuine question at the time, not a joke. The first problem was not -r or -f. That would not have helped me. The problem was rm foo vs rm foo/. Tab completion in bash will add the slash. That slash makes rm try to treat it as a directory. Without -r or -f it will fail, then the inexperienced will google the error message and be told to use -r. And loose data. Neither of the proposed related questions explain the / issue. As far as I can tell only the accepted answer here has that explanation, on the whole internet. Commented Sep 5, 2023 at 16:06

10 Answers 10

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

13
  • 137
    ah yes. that makes sense. I never typed foo, I typed f<tab> and bash filled in a / for me. Commented 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. Commented Oct 28, 2013 at 20:05
  • 23
    @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. Commented Oct 29, 2013 at 0:36
  • 31
    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. Commented Aug 8, 2016 at 13:05
  • 6
    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. Commented Aug 8, 2016 at 13:11
865

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.

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

rm should remove the symbolic link.

$ cd ~
$ mkdir bar
$ ln -s bar foo
$ ls -l foo
lrwxrwxrwx 1 skrall skrall 3 2008-10-16 16:22 foo -> bar

$ rm foo
$ ls -l foo
ls: cannot access foo: No such file or directory

$ ls -l bar
total 0
0
15

OUT OF DATE ANSWER. rm -d removed sometime between 2008 and 2023.

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.

2
  • 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
    Commented Jul 22, 2020 at 11:33
  • What system was it tested on? What shell (if relevant)? Commented Sep 3, 2023 at 21:07
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
  • 2
    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
    Commented Nov 4, 2020 at 8:34
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 I 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!

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.

0

You can use unlink <filename> in the folder where you have created your symbolic link.

1
  • 1
    Isn't this already covered? Commented Sep 3, 2023 at 21:13

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