Soft Links:-
We have seen hard links in our previous post. Hard links are nothing but more than one names for a file. Hard links have some shortcomings.i) We cannot hard link two files in different file systems, i.e. a file in /usr directory cannot be hardlinked in /home directory.
ii) Hard links cannot link directories even in the same file systems. It can link only files.
Soft links remove limitations of Hard links.
Now let's see what are Soft links.
We make a file "file1" as shown below[code]cat>file1
date
[/code]Assign execute permission to this file
[code]chmod u+x file1 [/code]
Now we create a symbolic link "file2" to this file as shown below
[code]ln -s file1 file2[/code]
Note: We are using -s attribute to create a symbolic link. If -s is not used then it would create a hard link.
Now, our symbolic link is created. You can verify the creation of symbolic link using
[code]ls -l file2[/code]
Output is:
lrwxr--r-- 1 user1 user1 2012-04-15 16:14 file2 -> file1
Note: l in the starting and "file2 -> file1" denote the creation of symbolic link. Also, number in the second column is 1. This means, a new file is created, as contrary to Hard links in which no new file is created.
Now we have two files. We know the content of file1 but What does file2 contains?
file2 contains the pathname of file1. When we execute cat command on file2, it actually redirects to file1 and opens its content.
So, cat file1 and cat file2, both show the same content. We can also execute the content using any of the file.
The output of ./file1 and ./file2 are same, i.e.
Sun Apr 15 16:18:28 EDT 2012
One advantage of soft link over hard link is that it can link directories also. Suppose we have to link a large number of files from one directory to another. If we use Hard link, then the only way is to link each file individually. But, when we use soft link, we can link an entire directory to another directory.
Caution: Hard link provides safety against accidental deletions, i.e. if one alias is deleted then we can access the file through other aliases.
But, in the case of symbolic link, we created above, if file2 is deleted then there is nothing to worry about. But, if by mistake file1 is deleted then all our content is lost. Also symbolic link file2 would become a dangling symbolic link.This is because symbolic link is a seperate file, and not a pointer to the original file.
For more information visit
Really nice article on symbolic links
ReplyDeleteDelete symbolic link in unix