b5media.com

Advertise with us

Enjoying this blog? Check out the rest of the Technology Channel Subscribe to this Feed

New Linux User

HOWTO: Remove a Symbolic Link

by Jon on October 31st, 2005

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.

POSTED IN: How To

38 opinions for HOWTO: Remove a Symbolic Link

  • Erik
    Jan 22, 2006 at 11:47 pm

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

  • Jon
    Jan 23, 2006 at 5:41 am

    Thanks Erik,

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

  • Mihai
    Feb 8, 2006 at 5:42 am

    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?

  • Jon
    Feb 8, 2006 at 8:04 am

    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
    Feb 16, 2006 at 1:15 pm

    use:

    unlink symbolic_link

    instead of rm.

  • Jon
    Feb 16, 2006 at 1:43 pm

    So easy…damn.

    Thanks bbobbo!

  • James
    Feb 20, 2006 at 4:45 pm

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

  • Karl
    May 3, 2006 at 1:23 pm

    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.

  • Jon
    May 4, 2006 at 9:28 am

    Excellent tip, Karl. Thanks!

  • Karthik
    Jun 8, 2006 at 11:56 pm

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

  • Andrew
    Jul 9, 2006 at 7:47 pm

    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…

  • Gunni
    Jul 31, 2006 at 7:50 am

    to remove symbolic links try ‘rm -r’

  • WarZeg
    Aug 8, 2006 at 12:11 am

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

  • Joseph
    Aug 16, 2006 at 7:08 am

    Great tip on the unlink command.

    Thanks a lot.

  • gemini728
    Aug 16, 2006 at 2:55 pm

    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?

  • fozner
    Sep 11, 2006 at 9:59 pm

    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
    Dec 19, 2006 at 9:00 pm

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

  • jafar
    Dec 20, 2006 at 2:56 pm

    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.

  • John Cooke
    Feb 22, 2007 at 8:02 am

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

    many thanks

  • TuVahLuh
    Mar 29, 2007 at 9:43 am

    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
    Jul 12, 2007 at 4:43 am

    thax bboobo solved my prob!

  • Giang
    Aug 16, 2007 at 6:51 pm

    EG:
    rm -r -f Folder

  • OMA
    Oct 20, 2007 at 9:36 pm

    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
    Nov 3, 2007 at 8:38 am

    Thank You Karl for the unlink!!

  • pr
    Dec 11, 2007 at 6:27 pm

    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
    Dec 11, 2007 at 7:33 pm

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

  • kkcamiie
    Dec 15, 2007 at 6:16 pm

    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
    Dec 28, 2007 at 10:57 am

    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.

  • Carl
    Jan 26, 2008 at 12:46 pm

    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
    Feb 29, 2008 at 11:00 am

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

  • ynaf
    Mar 13, 2008 at 6:43 pm

    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
    Mar 14, 2008 at 10:15 am

    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
    Mar 17, 2008 at 11:14 am

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

  • cizsfrancis
    Mar 26, 2008 at 6:01 am

    like kkcamiie said

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

    it work 100% for me…

  • cizsfrancis
    Mar 26, 2008 at 6:07 am

    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
    Apr 8, 2008 at 4:20 am

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

  • Rohit
    May 14, 2008 at 2:57 pm

    Thanks Karl. Great tip.

  • Ram
    May 15, 2008 at 10:59 pm

    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.

Have an opinion? Leave a comment:




Check Spelling
Activate Spell Check while Typing
Site Meter
Close
E-mail It