Matthew Scouten
Matthew Scouten

Reputation: 15561

Remove a symlink to a directory

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?

Upvotes: 1208

Views: 1044075

Answers (10)

Joshua
Joshua

Reputation: 43227

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.

Upvotes: 15

Keith Whittingham
Keith Whittingham

Reputation: 326

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!

Upvotes: 3

Joe Phillips
Joe Phillips

Reputation: 51100

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.

Upvotes: 869

R. Pandey
R. Pandey

Reputation: 19

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

Upvotes: 0

Steve K
Steve K

Reputation: 19586

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

Upvotes: 21

Matthew Scharley
Matthew Scharley

Reputation: 132254

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

Upvotes: 1487

sharmin sultana
sharmin sultana

Reputation: 1

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.

Upvotes: 0

rubel Mazumder
rubel Mazumder

Reputation: 49

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

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

Upvotes: 5

TJ L
TJ L

Reputation: 24452

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.

Upvotes: 5

Greg Hewgill
Greg Hewgill

Reputation: 992877

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.

Upvotes: 8

Related Questions