Before looking into the difference, we should look at “what is inode” in brief first.
What is inode:
inode(index node) is the data structure in the Unix-based file system which stores the metadata of the file and how and where the data of the file is stored. This includes information related to file size, permissions, groups, ownership, file type, file size, number of links, etc. Remember!! The inode doesn’t store the name of the file. file name information is always stored in the directory with its respective inode numbers.
Soft link:
Soft links are also referred to as symlinks. it is the windows equivalent of shortcuts. if you delete the original file, symlinks will not work. each symlink has a different inode.
Hard link:
It is another alias of the file, unlike the shortcuts. if you delete the original file, still hard links will work.
Let’s understand the differences by example and diagram.
testuser-mbp:~ testuser$ touch test.txt
testuser-mbp:~ testuser$ ls -li test.txt
12935258084 -rw-r--r-- 1 testuser staff 0 Sep 5 20:13 test.txt #inode of the file "test.txt" is 12935258084
testuser-mbp:~ testuser$ ln -s test.txt soft_link #Let's create a soft link.
testuser-mbp:~ testuser$ ls -li soft_link #Let's check inode number of the soft_link.
12935259053 lrwxr-xr-x 1 testuser staff 8 Sep 5 20:16 soft_link -> test.txt #inode of the soft_link is different from the file "test.txt" i.e. 12935259053
testuser-mbp:~ testuser$ ln test.txt hard_link #Let's create a hard link now.
testuser-mbp:~ testuser$ ls -li hard_link #Let's check inode number of the hard_link.
12935258084 -rw-r--r-- 2 testuser staff 0 Sep 5 20:13 hard_link #inode of the hard_link is the same as of the file "test.txt" i.e. 12935258084
Diagram:
Summarising the differences:
Soft links | Hard links |
---|---|
It has a different inode compared to the original file. | It points to the inode of the original file. |
Can be created on both files and directories both. | Can only be created with files and not directories. |
It is equivalent to the shortcuts on windows. | It is equivalent to another copy of the original file which points to the same inode of the file. |
Command ln -s file_name soft_link_name | Command: ln file_name hard_link_name |
Can be used across different file systems. | Cannot be used across different file systems. The reason is inode of a filesystem can not be shared across different file systems because their structure is different. |
If we delete the original file, soft links will not work. | If we delete the original file, still hard links will work. |
Soft links work with both absolute and relative paths. | Hard link only works with absolute path. |
Any changes in the soft link don’t impact the original file. | Any changes in the hard link impact the original file. |
It takes extra space. | it doesn’t need extra space. |