Skip to content

Sunday, December 6th, 2009

New Linux User

HOWTO: Remove a Symbolic Link

October 31, 2005 by Jon  
Filed under How To

This may seem not seem complicated, but if you don’t know, you don’ t know.

When you create a symbolic link, the link file you’ve created just points to the target file. Therefore, the link file has no other purpose in life than that. So, when you want to get rid of it, just delete the link file.

Done.

  • StumbleUpon
  • Digg
  • Facebook
  • Mixx
  • Google
  • TwitThis
  • Reddit
  • Yahoo! Buzz
  • Slashdot
  • E-mail this story to a friend!

Comments

58 Responses to “HOWTO: Remove a Symbolic Link”
  1. Erik says:

    Yes, but it may also be helpful to some to understand that the link to a directory is treated as a file, and not as a directory (which it simulates). Therefore, ‘rmdir’ accomplishes nothing. :)

  2. Jon says:

    Thanks Erik,

    I have never considered creating a symlink to a directory, but why not! Great tip :)

  3. Mihai says:

    When I run rm may_symbolic_link I get a error message like:

    rm: cannot remove `/destination/folder’: Is a directory

    I don’t want to remove the directory – just the link. Any ideas?

  4. Jon says:

    Hey Mihai,

    I’m not sure what’s going on there. I took a look at some other sites (like here) and it seems that you should be able to do what you want.

    Anyone else have any ideas?

  5. bbobbo says:

    use:

    unlink symbolic_link

    instead of rm.

  6. Jon says:

    So easy…damn.

    Thanks bbobbo!

  7. James says:

    bbobbo, you ‘da Man!!! I NEEDED that! Many thanks!

  8. Karl says:

    When using the rm or unlink command to remove a symbolic link to a directory, make sure you don’t end the target with a ‘/’ character because it will create an error. Example:

    $ mkdir dirfoo
    $ ln -s dirfoo lnfoo
    $ rm lnfoo/
    rm cannot remove directory ‘lnfoo/’ : Is a directory
    $ unlink lnfoo/
    unlink: cannot unlink ‘lnfoo/’: Not a directory
    $ unlink lnfoo
    $

    Notice how one complains it “Is a directory”, but the other complains it is “Not a directory”, which I found confusing. This is a problem if you have a tendency to use tab completion a lot, because it will stick a ‘/’ at the end.

  9. Jon says:

    Excellent tip, Karl. Thanks!

  10. Karthik says:

    Thanks Karl!
    I was having this problem for a long time.

  11. Andrew says:

    Karl – thanks for your post. I knew to use unlink, but could not for the life of me figure out why it wasn’t working!

    Pesky slashes…

  12. Gunni says:

    to remove symbolic links try ‘rm -r’

  13. WarZeg says:

    how about the user how can remove it if you accidentally use “userdel username” instead “userdel -r username”

  14. Joseph says:

    Great tip on the unlink command.

    Thanks a lot.

  15. gemini728 says:

    OK, none of the above worked for me. My case is a little more complicated. I created a mount point (I guess that is not a link) to an SMB share on a Windows machine on the network. Now, it appears the IP address of the Windows machine has changed, so I cannot get to that directory anymore. I created a new mount point and mapped it to the same machine and SMB share. But I cannot delete the old link/mount-point, or whatever it is considered.

    Any ideas?

  16. fozner says:

    Serve up a temporary share with the same name on the local machine. Use ifconfig x.x.x.x up to spoof the old IP. Now you can access something that looks like it. Well, it might not be the greatest but it’s an idea…

  17. Andy says:

    Thanks for the tip Karl! And Gunni, don’t be an ass.

  18. jafar says:

    Some time you may not be able to unlink or rm. Just renmae the source and then you can remove or unlink. then you can renmae to source to its original name. I hope this help.

  19. John Cooke says:

    The unlink command worked a treat on a symlinked directory I was having trouble deleting

    many thanks

  20. TuVahLuh says:

    I tried the unlink after google directed me here….

    I said to my linux…

    unlink ligh (and then pressed tab for autocompletion)

    Then I got:

    unlink: cannot unlink `lighttpd-main/’: Not a directory

    Aha! The trailing slash using autocomplete messes it up. This time get rid of trailing slash and it unlinked just fine.

  21. booobbo rocks says:

    thax bboobo solved my prob!

  22. Giang says:

    EG:
    rm -r -f Folder

  23. OMA says:

    CAUTION!: DON’T RUN the commands Giang and Gunny posted. Doing a “rm -r” will do a recursive deletion removing the actual files that the link points to. If you just want to remove the link, DO NOT use rm-r

  24. Isha says:

    Thank You Karl for the unlink!!

  25. pr says:

    is there a way to make rm act only on regular files without affecting symbolic links? i.e., i want to do “rm -f *” but i dont want any of the symbolic links to be deleted. thanks in advance. – pr

  26. Clair says:

    @pr what do you mean by you don’t want the symbolic links to be deleted? in what cases? :)

  27. kkcamiie says:

    I’v just installed Ubuntu Server 7.10 today, and this is the first time im throwing myself into linux. But the only solution that worked for me was actually ‘rm -r /path/to/symlink_folder/’ which btw deleted NONE of my files in the folder it was linking too. Can anyone explain how this is true, now since you actually posted warnings against the using of that method?

  28. nph says:

    I have an XO laptop and I attempted to install the java plugin (http://wiki.laptop.org/go/RestrictedFormats). everything went OK until I set up the symbolic link. I misspelled one of the folder names. I can’t remove the symbolic link because permissions are denied at the root level. any suggestions? I can’t remove or unlink. I tried to redo the link statement but it will not allow me to overwrite the link.

  29. Carl says:

    In all fairness to Gunni, rm -r worked for me without actually deleting sub-directories as I suspected it could but nothing else worked (I made a backup first though).

    The thing is that there are so many different paths people can take to creating their symbolic links in the first place. In my project I mixed up source and destination directories.

  30. EB says:

    $ mkdir dirfoo
    $ ln -s dirfoo lnfoo
    $ rm lnfoo
    $
    Success.
    Don’t know about anyone else, but for me using unlink is unnecessary.

  31. ynaf says:

    Like ‘pr’ a few posts ago, I am also looking for a way to be able to do a rm -f * in a directory without deleting the target of my symbolic links.
    Is there a way to protect the symbolic links, or something link that.

  32. pr says:

    Here is what I found. For example, if you want to remove, say all files that end in .txt in all the sub directories but which are NOT symbolic links, do the following

    find . -name “*.txt” ! -type l -exec rm {} \;

  33. Yossarian says:

    On MacOSX Darwin, I can confirm that ‘rm -r’ will indeed delete the linked directory if use it to delete a symbolic link.

  34. cizsfrancis says:

    like kkcamiie said

    try to “rm /path/to/sysmbolic/link” this will help you..

    it work 100% for me…

  35. cizsfrancis says:

    because the system think that the symbolic link “~francis” is a home directory, that’s why it always says that the file is a directory or something else… for example if you type “cd ~francis” it will go to the home of francis (/home/francis)…

    so the best way to do to delete the symbolic link is to delete the file by path

    ie. “rm /var/www/html/~francis” this is the right way

    DONT DO “rm -rf ~francis” or “rm ~francis” it will delete the directory

    i hope this will help you guys

  36. Carlos Eduardo says:

    use : rm -f you_sysmbolic_link
    not use /, ex.: rm -f you_symbolic_link/ ( ERROR )

  37. Rohit says:

    Thanks Karl. Great tip.

  38. Ram says:

    Hi, guys this Ram, i am very happy who send the solution to the comment for “How to remove Symbolic_link to a file” bcoz, it helped me a lot to remove the link. Once again i am thank u very much.

    And guys i wanna a join into this forum, can u tell me way how i can join in this.

  39. Fidel says:

    I was having the same problem.
    In BASH, try
    # rm “linktodir”
    It seems like a “/” is automatically added if no quotes are used, thus causing the shell to reference the directory being pointed-to instead of the pointer.

  40. F4jr says:

    Thanks, this can avoid some mistakes !

  41. Thanks bbobbo, you have no idea how long that’s been bugging me! I had a few directories laying around that I couldn’t delete because of those stupid symbolic links, but now all is well! :)

  42. Ed says:

    I’m having an issue with creating a link Too many levels of symbolic links, how do I fix this?

  43. david says:

    If I run a program when my shell is at its directory, it runs fine. But if I run it via the symbolic link, the program itself complains about not finding some files. I wonder if running a symbolic link is “as if” running the program from where its directory is at – in my case, not!? How do I run my program from any location I want (btw, I stored the symbolic link in a directory opt/bin which is a existing PATH. My other symbolic links are in the opt/bin too and they run fine. Only this one program “ClustalX” cannot find things … )

    Help, thanks!

  44. adam says:

    nice resource you have here … I had problems with deleting the symbolic link too – unlink wasn’t working. Until one reader mentioned the tab completion was adding the trailing slash indicating it is a directory … this finally worked … unlink name_pointing_to_a_directory BUT DON’T ADD THE TRAILING SLASH.

    :)

  45. jack oniel says:

    Saved my bacon with the unlink command, thanks!

  46. Mike_S says:

    The tab-completion-trailing-slash was also the problem with my attempts to rm / unlink the sym links.

    .. spoiled by the tab completion :) .. though it really is a neccessary and vital shell tool when navigating the typical mult/levelled/directory/structure/found/in/most/unix/applications !

  47. Farhan says:

    thanks bbobbo ur tip solved my problem.

  48. jesblanc says:

    hello all,

    maybe someone can help me here.

    i created a symlink, which points the website store.islandcargosystems.com to icsmarketplace.com. it works great except that i cannot understand why it kills the links on my flash file.

    anybody know anything about this? im thinking of going a different route and creating a mod_rewrite.

    thanks in advance

  49. English says:

    In English, you’ve effectively said nothing:

    “When you create a symbolic link, the link file you’ve created just points to the target file. Therefore, the link file has no other purpose in life than that. So, when you want to get rid of it, just delete the link file.”

    The point is the last it in the paragraph. The preceding sentence references the link file points to the target file. If you want to get rid of it…

    The it could mean either the target file, in which case you are stating that you can remove targetfiles by removing symlinks, or removing the symlink, the desired behavior.

    The correct way to write this in English is not to use ‘it’. While there is a ‘correct’ way of interpreting *it*, all style books point to not using it, and referring to the noun.

    “When you create a symbolic link, the link file you’ve created just points to the target file. Therefore, the link file has no other purpose in life than that. So, when you want to get rid of the symbolic link, just delete the link file. This will not delete the target file.”

    Now, I’ve used two precedents here. I’ve gone for plain English, but in addition, I’ve used tenets of technical writing, where you answer a question rather than state a fact.

    The additional sentence answers the question in a users mind, and completes the thought process.

    If you re-read your posting, you will see how you failed to tackle the true nature of the post.

    • BartO says:

      This is so useless. These forums are read by people all over the world, most of the non-native English speakers, like me. While your alternative might be better, I got the meaning of the first sentence just as well. And that’s what it’s all about. Getting an idea across. Not writing it down in the best possible way. That would be totally waisted on me, and probably on most non-native English speakers.

  50. Hoaobrook says:

    English, you sound like a frustrated English teacher. I imagine you’ve spent many a days grading sub-par English papers, likely just before this post. I understand your frustration, however, publicly scorning the original poster for sharing knowledge is not the most efficient way of becoming part of a community online.

    If you re-read your posting, you will see how you failed to tackle the true nature of the post.

Trackbacks

Check out what others are saying about this post...


Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!


About Us | Advertise with us | Blog for EveryJoe | Privacy Policy | Terms of Use
Get This Theme | Sitemap


All content is Copyright © 2005-2009 b5media. All rights reserved.