2012/12/29

Linux Symbolic links: Soft and Hard links

What is a symbolic link? Why its important ?

Symlink (Symbolic link) is a reference to another file or directory. It allows you to point multiple file names to single inode that points to a physical block address on the disk.

Symlink's  are mostly used as a shortcut's, they can make your life a lot easier by making your directories of data available for you where you want it.

How do i make a symlink ?

Use the following option in ln command:

 -s, --symbolic
              make symbolic links instead of hard links

For example:

 ln -s file1 file1_symlink  
 ls -l
 -rw-r--r-- 1 m m 0 2012-12-29 14:57 file1  
 lrwxrwxrwx 1 m m 5 2012-12-29 14:58 file1_symlink -> file1  


How do i make a hard link ?


 ln file1 file1_hardlink  
 ls -l  
 -rw-r--r-- 2 m m 0 2012-12-29 15:03 file1  
 -rw-r--r-- 2 m m 0 2012-12-29 15:03 file1_hardlink  

What is the difference between soft and hard link ?

Most important difference between hard and soft is that soft link depends on the original file.

For example, if i create a soft link and delete the original file that i linked, i cannot access my file trough symlink anymore and it becomes broken link.

 m@srv:~/symlink_test$ cat file1_symlink   
 Hello World!  
 m@srv:~/symlink_test$ rm file1  
 m@srv:~/symlink_test$ cat file1_symlink   
 cat: file1_symlink: No such file or directory  

But in the case of hard link, my file is always accessible if there is one hardlink existing of the file

 m@srv:~/symlink_test$ cat original  
 Hello World!  
 m@srv:~/symlink_test$ ln original hard_linked  
 m@srv:~/symlink_test$ cat original hard_linked   
 Hello World!  
 Hello World!  
 m@srv:~/symlink_test$ rm original   
 m@srv:~/symlink_test$ cat hard_linked   
 Hello World!  

Also hard links do not link paths on different volumes or file systems, soft links do. Soft links also consume inodes but hard links always shares the same inode.

Where is the symbolic link stored ?

Symlinks are just references to a physical location of a file, if you make symlinks, its doesn't meant that you are losing disk space by creating a copies. Symbolic links are stored in the same place as inodes, inodes are the place where disks resides its block addresses. 

No comments:

Post a Comment