216

I was trying to create this symbolic link:

sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin

but I accidentally typed:

sudo ln -s /usr/share/php,yad,in /var/www/phpmyadmin

So now I want to correct it but it says symbolic link already exist.

Braiam
  • 67,791
  • 32
  • 179
  • 269
James
  • 2,169
  • 2
  • 13
  • 3

6 Answers6

240

You can use rm to delete the symlink.

Example:

-rw-rw-r-- 1 2014-01-02 09:21 tmo
lrwxrwxrwx 1 2014-01-02 09:21 tmo2 -> tmo

Then ...

 rm tmo2

will remove the symlink.

Rinzwind
  • 299,756
  • 1
    permission denied. This is what i typed first: sudo ln -s /usr/share/php,yad,in /var/www/phpmyadmin – James Jan 03 '14 at 05:19
  • 7
    permission denied: You do know you need sudo if you want to use it in a situation you do not own the file? That goes for 'rm' too. – Rinzwind Jan 03 '14 at 06:32
  • 2
    i did it rm /usr/bin/python and removed my python from ubuntu :S –  Jul 23 '16 at 19:17
  • 2
    @RaheelKhan no you did -not- You removed a SYMLINK. Python relies on this symlink though. If you recreate that symlink python will be back. – Rinzwind Jul 23 '16 at 19:29
  • 1
    I try this command but it seems that my file has been removed, I don't want to remove my file – saeed masoomi Dec 11 '17 at 15:52
  • 1
    @saeedmasoomi in the example "rm tmo" removes the file. "rm tmo2" removes only the symlink. Make sure you remove the symlink (the one with the "l" at the front) – Rinzwind Dec 11 '17 at 16:15
  • my output is like this lrwxrwxrwx 1 saeed saeed 28 Dec 11 21:51 libGL.so -> /usr/lib/nvidia-173/libGL.so and the second path doesn't exist so i stuck in this problem, thanks for your answer, what should be my command in this situation, if I use `sudo rm libGL.so then this file will be removed:( – saeed masoomi Dec 11 '17 at 19:30
  • 1
    If you get error: rm: cannot remove dir/: Is a directory, remove the trailing /. – subtleseeker May 26 '20 at 08:41
  • This is the only answer that works (Rinzwind) – manowar_manowar Jan 11 '23 at 18:38
46

You can try the unlink command as well.

unlink is a similar command to rm. Therefore rm <symlink> will work same as unlink <symlink>

Here is the man page.

Zanna
  • 70,465
hakunami
  • 655
  • 15
    unlink has nothing to do with symlinks in particular. See http://serverfault.com/a/38817/64085 – Matthew Read Apr 15 '15 at 16:24
  • 1
    While you are correct that unlink will remove the symlink, it is not an alias of rm. They are different, if ever so slightly. For one you cannot pass multiple arguments to unlink – Jarad Downing Feb 17 '20 at 19:49
17

Suppose you were trying to do:

sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin/

but accidentally did:

sudo ln -s /usr/share/somethingelse /var/www/phpmyadmin/

To correct it simply navigate to the folder where the link is and unlink

cd /var/www/phpmyadmin/  
~:# unlink somethingelse
Ravan
  • 9,379
7

You can use the following to remove the symbolic link

sudo rm /usr/share/php,yad,in

Explanation

  • rm is the terminal command to remove a file. See rm --help for more options that it can take.
  • sudo is used because the symbolic link was created with sudo. The file therefore belongs to root and your normal user will not have permission to edit/remove it (you would be able to force this if you had write permission on the directory, which would not be the case here).

Extra

Also see this post and my comment to the first answer to access phpmyadmin when getting a not found error after install.

Zanna
  • 70,465
chesedo
  • 1,709
  • 14
  • 25
3

A small caveat I found was that I was trying to run rm and unlink on a symlink and I was getting an error that it was a directory.

$ rm folder_name/
rm: cannot remove ‘folder_name/’: Is a directory
$ unlink folder_name/
unlink: cannot unlink ‘folder_name/’: Not a directory

To remove the symlink, I used unlink folder_name. It was failing as there was a trailing / which causes the file to appear to be a directory.

Zanna
  • 70,465
  • Yeah, the key thing is trailing slash / at the end, which needs to be removed. Command rm works as well. – М.Б. Jan 16 '20 at 01:15
-1

I stumbled here because I had to remove a dpkg-divert and the new package won't install until it was removed.

So if you have done something like this:

sudo dpkg-divert --add --rename --divert /usr/bin/gcc.real /usr/bin/gcc

You need to remove it with something like this:

sudo dpkg-divert --remove /usr/bin/gcc.real
Zanna
  • 70,465