Create and Delete Symbolic Link

In computing, a symbolic link (often shortened to symlink and also known as a soft link) consists of a special type of file that serves as a reference to another file or directory. Unix-like operating systems in particular often feature symbolic links.

I. CREATE SYMBOLIC LINK

To create a symbolic link in Unix, at the shell prompt, enter the following command:
[code]
ln -s target_filename symlink_filename
[/code]
Replace target_filename with relative or absolute path to which the symlink will point to. Usually the target will exist, although you can create a symlink to a target which does not exist. Replace symlink_filename with the desired name of the symbolic link. The ln command then creates the symbolic link. After you've made the symbolic link, you can then treat symlink_filename as an alias for the target file. You can use normal file management commands (e.g., cp, rm) on the symbolic link. Commands which read or write the file contents will access the contents of the target file.
Example:
[code]
[root@wowtutorial /home/donie]# ls -al ../files/
total 14
drwxrw-rw- 6 donie nobody 512 Oct 8 15:40 .
drwxr-xr-x 7 donie donie 512 Oct 8 15:48 ..
-rwxrw-rw- 1 donie nobody 93 Oct 8 15:40 .htaccess
drwxrw-rw- 2 donie nobody 512 Oct 8 15:40 pictures
drwxrw-rw- 2 donie nobody 512 Sep 29 23:47 u1
drwxrw-rw- 2 donie nobody 512 Oct 8 15:40 u2
drwxrw-rw- 2 donie nobody 512 Oct 8 15:40 u6
[root@wowtutorial /home/donie]# ln -s ../files test
[root@wowtutorial /home/donie]# ls -al
total 164
drwxr-xr-x 9 donie nobody 1024 Oct 10 03:22 .
drwxr-xr-x 7 donie donie 512 Oct 8 15:48 ..
-rw-r--r-- 1 donie nobody 589 Sep 30 05:15 .htaccess
-rw-r--r-- 1 donie nobody 29527 Jul 26 12:16 CHANGELOG.txt
-rw-r--r-- 1 donie nobody 1431 Sep 8 2006 INSTALL.mysql.txt
-rw-r--r-- 1 donie nobody 1073 Sep 8 2006 INSTALL.pgsql.txt
-rw-r--r-- 1 donie nobody 9263 Jul 25 22:29 INSTALL.txt
-rw-r--r-- 1 donie nobody 18064 Jul 9 2006 LICENSE.txt
-rw-r--r-- 1 donie nobody 1778 Dec 11 2006 MAINTAINERS.txt
-rw-r--r-- 1 donie nobody 2941 Jan 9 2007 UPGRADE.txt
-rw-r--r-- 1 donie nobody 262 Aug 9 2006 cron.php
-rwxr-xr-x 1 donie nobody 1150 Sep 29 19:32 favicon.ico
lrwxr-xr-x 1 donie nobody 8 Oct 10 03:22 test -> ../files
[root@wowtutorial /home/donie]# ls -al test/
total 14
drwxrw-rw- 6 donie nobody 512 Oct 8 15:40 .
drwxr-xr-x 7 donie donie 512 Oct 8 15:48 ..
-rwxrw-rw- 1 donie nobody 93 Oct 8 15:40 .htaccess
drwxrw-rw- 2 donie nobody 512 Oct 8 15:40 pictures
drwxrw-rw- 2 donie nobody 512 Sep 29 23:47 u1
drwxrw-rw- 2 donie nobody 512 Oct 8 15:40 u2
drwxrw-rw- 2 donie nobody 512 Oct 8 15:40 u6
[root@wowtutorial /home/donie]#
[/code]
Do you get it? When we did ls -al ../files is same as ls -al test/

II. DELETE SYMBOLIC LINK

Be careful if you want to delete symbolic links by using rm command.
If the link is pointed to a file, you can safely delete the link using rm, but if it's pointed to a directory,
do not use the trailing backslash behind the link name!
If you do rm -rf test/, may God forgive you.
Instead of using rm, you can safely remove symbolic link by using unlink.
[code]
[root@wowtutorial /home/donie]# unlink test
[/code]
As easy as it seems, huh?

Facebook Fans