How to Remove a Symbolic Link (Symlink)

May 25, 2016 - 8 Comments

Remove a symbolic link with unlink or rm command

Removing a symbolic link is achieved through the command line, and as we’ll show you, there actually two different ways to undo a soft link. This is aimed at users who spend a lot of time at the command line, but for the less familiar, symbolic links are used in Linux, Mac OS X, and Unix to point one location or file to another location or file, much like how an alias works on Mac OS X Finder or a shortcut works in Windows desktop.

Let’s jump right to it and demonstrate how to delete a symlink.

And yes, this works to delete a symlink in Linux, Mac OS X, or any other modern Unix based operating system.

Remove a Symbolic Link with unlink

The best way to remove a symlink is with the appropriately named “unlink” tool. Using unlink to delete a symlink is extremely simple, you just need to point it at the symbolic link to unlink and remove. As always with the command line, be sure your syntax is precise.

unlink SymLinkToRemove

Whether the symbolic link is to a file or a link to a directory, it does not matter, just point directly at the symlink in question and do not append the / trailing slash at the end.

How to remove a symlink

For example, if we were removing a symbolic link from ~/Desktop/hosts to /etc/hosts then you would do the following:

cd ~/Desktop/

unlink hosts

You can always confirm that you’re looking at a symbolic link with the ‘ls -l’ command like so:

ls -l
-rwxr-xr-x 1 Paul staff 24K Jun 19 11:28 hosts -> /etc/hosts

That will tell you where the symlink is pointing to if you aren’t certain.

The unlink command is basically the rm command, which you can also use if you want to remove a symbolic link.

Delete a Symlink with rm

You can also use the rm command directly to remove symbolic links. If you aren’t really comfortable with using this approach, you can always enable a confirmation with rm and srm commands before they run, which is helpful for novices to the command line or those with notoriously poor syntax accuracy.

rm SymLinkToDelete

Much the same as unlink, be sure you’re pointing at the proper symbolic link and do not include a directory / when specifying the symbolic link to remove, it’s a link and not a real directory after all.

Remove a symbolic link with unlink or rm command

Ultimately it doesn’t matter much which method you choose to remove a symbolic link, just go with what you remember or are comfortable with.

Terminal

Know of another or better way to modify and delete symbolic links at the command line? Let us know in the comments.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

8 Comments

» Comments RSS Feed

  1. Brian says:

    Note that this will not work for directory symlinks (junctions). You will need to use the rmdir command for that. This should be added to this how-to since I’d guess a large majority of the time symlinks will be to directories, not files.

  2. Storm says:

    I’m probably missing something here, but wouldn’t it just be easier to delete a symlink using the Finder?

    • Adam says:

      You can definitely use the Finder to delete symlinks. However, it is nice to know how to do it from the terminal, which is why this article exists.

  3. Johnny says:

    Why remove the link in the first place when it´s only symbolic?

    • NTn says:

      If you no longer need a symlink, you would remove it. Otherwise the symlink stays in effect.

      For example, if you no longer want to be backing up an iPhone to an external hard drive, or linking an iTunes library to another volume.

  4. rb says:

    Kind of obvious for any longtime command line user but if you use ‘unlink’ on a normal file it will delete it, because ‘unlink’ is an alias to ‘rm’

    Novices probably should not be in the terminal app anyway.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site