HOWTO: Remove a Symbolic Link

Posted in Technology
Mon, Oct 31 - 11:23 am EDT | 9 years ago by
Comments: 64
Share This Post:
  • Facebook
  • StumbleUpon
  • Tumblr
  • Reddit
  • Twitter

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.

Related Posts

Share This Post:
  • Facebook
  • StumbleUpon
  • Tumblr
  • Reddit
  • Twitter
  • Erik

    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. :)

  • http://www.newlinuxuser.com Jon

    Thanks Erik,

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

  • Mihai

    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?

  • http://www.newlinuxuser.com Jon

    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?

  • bbobbo

    use:

    unlink symbolic_link

    instead of rm.

  • http://www.newlinuxuser.com Jon

    So easy…damn.

    Thanks bbobbo!

  • James

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

  • Karl

    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.

  • http://www.newlinuxuser.com Jon

    Excellent tip, Karl. Thanks!

  • Karthik

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

  • Andrew

    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…

  • http://klam.is Gunni

    to remove symbolic links try ‘rm -r’

  • WarZeg

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

  • Joseph

    Great tip on the unlink command.

    Thanks a lot.

  • gemini728

    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?

  • http://comptune.com fozner

    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…

  • Andy

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

  • jafar

    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.

  • http://www.johncooke.info John Cooke

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

    many thanks

  • TuVahLuh

    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.

  • booobbo rocks

    thax bboobo solved my prob!

  • Giang

    EG:
    rm -r -f Folder

  • OMA

    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

  • Isha

    Thank You Karl for the unlink!!

  • pr

    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

  • Clair

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

  • kkcamiie

    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?

  • nph

    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.

  • http://www.projects-inprinciple.com Carl

    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.

  • EB

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

  • ynaf

    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.

  • pr

    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 {} \;

  • Yossarian

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

  • cizsfrancis

    like kkcamiie said

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

    it work 100% for me…

  • cizsfrancis

    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

  • Carlos Eduardo

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

  • Rohit

    Thanks Karl. Great tip.

  • Ram

    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.

  • Fidel

    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.

  • http://www.photos-china.com/ F4jr

    Thanks, this can avoid some mistakes !

  • http://www.zanderchance.com Zander Chance

    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! :)

  • Ed

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

  • david

    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!

  • http://ringtones-ringback-tones.com adam

    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.

    :)

  • jack oniel

    Saved my bacon with the unlink command, thanks!

  • Mike_S

    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 !

  • Farhan

    thanks bbobbo ur tip solved my problem.

  • http://icamarketplace.com jesblanc

    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

  • English

    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.

  • Hoaobrook

    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.

  • Hoaobrook

    JON, thanks for posting. I preferred the unlink method as any RM commands have to be validated by the administrator here – why? we had a newbie accidently run an rm -rf from root :( – at any rate, good on ya for posting this helpful bit. I was a little worried about running rm on the link thinking it might follow the link and delete the files ( I had the link on a folder).

  • BartO

    Ok, symbolic links are just really weird. Not only the rm -rf command follows the link, just rm seems to do it as well. That is, it doesn’t do anything for it thinks the link is a dirictory, and non-empty directories can’t be removed by just rm. But when I delete the link by pushing shift-del in nautilus, the link is removed whitout doing harm to the directory it pointed to. Both are done as root. I don’t get it…

  • Charlie

    on FC6 at least, your cwd has to be the directory in which the link was created for rm link to work.

    this is implied by whitehorn’s post..

  • Pingback: Ubuntu 9.04 Server einrichten (Teil 1: LAMP-Server) » Arsenal of Wisdom

  • Ben

    Thanks, for some reason I was scared the remove might delete the target file!

  • B

    Thanks Jon. The issue for me was I wanted to make sure doing “rm” on the sym link didn’t harm the actual target of the link.

  • Michael

    Maybe if ‘English’ takes some serious meds he/she might get some friends? What a hickhead.

  • Lucas

    Thanks again, Karl!

  • Pothi Kalimuthu

    Thanks for sharing. The trailing slash wasted around 10 minutes of my time. :) Learned a lesson. ;-)

  • BartO

    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.

  • whitethorn

    I just had the same problem, the mistake was using

    rm nameofsymlink/
    what you have to run is
    rm nameofsymlink

    without the backspace, the first one is trying to get rid of the folder being pointed to, while the second is just using the name of the file.

  • RossyMole

    Thanks for the litte bit of “extra” help there! Didn’t really know that bit..

  • http://twitter.com/fizz Saggi Malachi

    I was going ‘WTF?!’ for like 5 minutes while trying to do something I’ve done so many times in the past.
    Being so lazy after a long day I just kept hitting the tab key for auto-complete (which has ofcourse added the trailing slash)

    Thanks!

  • p2grace

    That did the trick – thanks!