How To Remove Symbolic Link In Linux With Example

May 13, 2011 | By
| Reply More

Symbolic links or Soft links work like pointers to another file. Note that there is only one copy of the actual file on the hard disk and in this way you can save valuable hard disk space by simply creating a link to it. Deleting a symbolic link is the same as removing a real file or directory.

Symbolic Links

1. Links have different inode numbers.
2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Link contains the path for original file and not the contents.
4. Removing soft link doesn't affect anything but when the original file is removed, the link becomes a 'dangling' link that points to nonexistent file.

Symbolic links can be removed with two commands: rm and unlink. You can use any one of the following commands to remove Symbolic links.

1. rm - removes each given FILE including Symbolic links
2. Unlink - deletes a single specified file name including Symbolic links.

Delete Symbolic Link File

The two commands given below are used to delete link:

# rm linkname
or
# unlink linkname

Example

To create Soft Link:

# ln -s /etc/resolv.conf dns

To list how Soft Link looks:

# ls -l dns
Outputs:
lrwxrwxrwx 1 bob bob 16 2009-08-16 04:28 dns -> /etc/resolv.conf

Now, to delete Symbolic Link dns created above:

# rm dns
OR
# unlink dns

Delete Symbolic Link Directory

The two commands given below are used to delete the link directory:

# rm linkDirName
or
# unlink linkDirName

Example

To create Soft Link:

# ln -s /etc link1

To list how Soft Link looks:

# ls -l link1
Sample Output:
lrwxrwxrwx 1 bob bob 4 2009-08-16 04:31 link1 -> /etc

Now to delete the test Symbolic Link directory:

# rm test

or

# unlink test

When using the rm or unlink command to remove a symbolic link from a directory, make sure you don’t end the target with a ‘/’ character because that 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
$

Filed Under : LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.