Difference between Softlink and Hardlink – Unix/Linux
Command to link files:
# ln
Both softlink and hardlink is used to make links between files/directories. Here is some common diferences between soft and hard links with examples.
I : Hardlink
1, The ‘ln‘ command by default create hard links.
Syntax:
# ln $Sourcefile $Link [root@localhost home]# touch crybit [root@localhost home]# echo "Hardlink test" > crybit [root@localhost home]# cat crybit Hardlink test
Create a hardlink of this file to a file in your / location:
[root@localhost ~]# ln /home/crybit link
[root@localhost ~]# cat link
Hardlink test
2, Hardlink not allowed for directory:
Proof:
Create a test diretory under /home location:
Create a test diretory under /home location:
[root@localhost home]# mkdir hard
Try to link that directory:
[root@localhost ~]# ln /home/hard/ link
ln: `/home/hard/': hard link not allowed for directory
3, Original file and linked will have the same inode number:
Proof:
You can findout the inode value by using the ‘ls’ command with ‘-i’ switch:
You can findout the inode value by using the ‘ls’ command with ‘-i’ switch:
# ls -li $file
[root@localhost ~]# ls -li /home/crybit
147144 -rw-r--r--. 2 root root 0 Jan 31 04:48 /home/crybit
[root@localhost ~]# ls -li link
147144 -rw-r--r--. 2 root root 0 Jan 31 04:48 link
In the abowe example the inode value is “147144“.
4, We can access, if the original file is removed/moved from server.
Proof:
Remove the original file:
Remove the original file:
[root@localhost home]# cat crybit
Hardlink test
[root@localhost home]# rm -rf crybit
Access the link file:
[root@localhost home]# cd
[root@localhost ~]# cat link
Hardlink test
5, Pointing link to the data location, that’s why we can access if the originalfile is removed from the location.
6, Hardlink is not possible in different partition.
Proof:
1, Create a test file in one partition:
1, Create a test file in one partition:
[root@localhost boot]# touch test
2, try to link from another location(Partition):
[root@localhost boot]# cd
[root@localhost ~]# ln /boot/test link
ln: creating hard link `link' => `/boot/test': Invalid cross-device link
II : Softlink
1, The ‘ln’ command with switch ‘s’ is used to create the softlink
Syntax:
# ln -s $Sourcefile $Link [root@localhost ~]# cd /home/ [root@localhost home]# touch crybit [root@localhost home]# echo "Softlink test" > crybit
Create softlink from another location:
[root@localhost home]# cd
[root@localhost ~]# ln -s /home/crybit link
[root@localhost ~]# ll link
lrwxrwxrwx. 1 root root 12 Jan 31 13:10 link -> /home/crybit
[root@localhost ~]# cat link
Softlink test
2, We can softlink directry also
Proof:
Create a test directory in /home location:
Create a test directory in /home location:
[root@localhost home]# mkdir test
Create a soft link to /home/test directory from another location:
[root@localhost ~]# ln -s /home/test/ linkdir
------
lrwxrwxrwx. 1 root root 11 Jan 31 13:15 linkdir -> /home/test/
------
3, Original file/directory and the linked file/directory are having different inode value.
Proof:
[root@localhost home]# ll -i crybit
147144 -rw-r--r--. 1 root root 14 Jan 31 13:10 crybit
[root@localhost home]# cd
-----
[root@localhost ~]# ll -i link
525994 lrwxrwxrwx. 1 root root 12 Jan 31 13:10 link -> /home/crybit
4, We can’t access, if the original file/diris removed/moved from server.
Proof:
[root@localhost home]# rm -rf crybit
[root@localhost home]# cd
[root@localhost ~]# cat link
cat: link: No such file or directory
5, Pointing link to the file/dir name, that’s why we can’t access if the original file/dir is removed from the location.
6, Softlink is possible in different partition.
Proof:
[root@localhost ~]# cd /boot/
[root@localhost boot]# mkdir test
[root@localhost boot]# cd
[root@localhost ~]# ln -s /boot/test/ linkdir
[root@localhost ~]# ll -d linkdir
lrwxrwxrwx. 1 root root 11 Jan 31 13:31 linkdir -> /boot/test/
These are the common diference between hardlink and softlink..!
No comments:
Post a Comment