Thursday, March 14, 2013

How to Delete Symbolic Link - Unix / Linux

I have created a new symbolic link to a directory. Now i want to remove it. How can i remove the symbolic link?

Before we actually see how to delete a symbolic link, let see creating the symbolic to a directory. Follow the below steps for creating and deleting symbolic link:

1. Create New Directory

I am creating a new directory, “linkdir”, in /var/tmp. Later I will create a symbolic link to this directory.

> pwd

>mkdir /var/tmp/linkdir

2. Creating symbolic link

The ln command in unix or linux is used to create a symbolic link.

> pwd

> ln -s /var/tmp/linkdir linkname

>ls -l
total 16
-rw-rw-r-- 1 mark mark  0 Aug 29 23:45 doc
lrwxrwxrwx 1 mark mark 16 Aug 30 00:43 linkname -> /var/tmp/linkdir
-rw-rw-r-- 1 mark mark 14 Aug 29 22:31 sample

You can observe from the output of the ls -l command, that a symbolic link has been created.

3. Deleting a symbolic link

In Unix or linux system everything is a file. Just as we remove the files using the rm command, in the same way remove the link.

> rm linkname

> ls -l
total 12
-rw-rw-r-- 1 pcenter pcenter  0 Aug 29 23:45 doc
-rw-rw-r-- 1 pcenter pcenter 14 Aug 29 22:31 sample

Note that, the rm command just removes the link. However it will not delete the target directory /var/tmp/linkdir. Another way of removing the symbolic link is to use the unlink command.

unlink linkname

No comments:

Post a Comment