Remove a symbolic link with unlink or rm commandRemove 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 symlinkHow 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 commandRemove 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.

TerminalTerminal

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

Source

Follow Me:
Top Best Sellers!!