Create symbolic link and unlink it

How to

This article will show how to create symbolic link and how to unlink it. Symbolic link also know as symlink or soft link is a special type of file which contains a reference to another file or directory in the form of relative or absolute path and act as pointer to other file.

At first I created a test enviroment in /tmp which looks as follow.

#
# mkdir linkdir && echo "test text" > linkdir/test.txt
#
# ls -la linkdir
total 4
-rw-r--r-- 1 root root 10 Sep 5 10:03 test.txt
#

How to create symbolic link

Let to show a little bit of theory. For creation of link we using command “ln” if we want create symbolic link option “-s” is needed. After command and option follow target path where we want to point link and then link it self. All together it looks like this:

“ln -s <target path> <link>”

#
# ln -s /tmp/linkdir link
#

To list link we use just “ls -la” command.

# ls -la link
lrwxrwxrwx 1 root root 12 Sep  5 10:04 link -> /tmp/linkdir
#

If you want to see content of target directory just put slash at the end of link.

#
# ls -la link/
total 12
drwxr-xr-x  2 root root 4096 Sep  5 10:03 .
drwxrwxrwt 10 root root 4096 Sep  5 10:04 ..
-rw-r--r--  1 root root   10 Sep  5 10:03 test.txt
#

As you can see in example below, thru link you can access directly its content.

# cat link/test.txt
test text
#

Unlink/remove symbolic link.

There are two ways how to remove link.

At first you can use “unlink” command.

#
# unlink link
#
# ls -la link/test.txt
ls: cannot access link/test.txt: No such file or directory
#

Or removing reference by “rm” command.

#
# rm link
#
#
# ls -la link/test.txt
ls: cannot access link/test.txt: No such file or directory
#

Leave a Reply