The links in Unix are essentially the pointers which associate to the files and directories. The major difference between a hard link and soft link is that hard link is the direct reference to the file whereas soft link is the reference by name which means it points to a file by file name.
Hard link links the files and directories in the same file system, but the Soft link can traverse file system boundaries.
Before understanding links we should first understand inode, an inode is a data structure comprised of metadata about the file such as file creation date, file authorizations, owner of the file and more.
Content: Hard link Vs Soft link
Comparison Chart
Basis for comparison | Hard link | Soft link |
---|---|---|
Basic | A file can be accessed through many different names known as hard links. | A file can be accessed through different references pointing to that file is known as a soft link. |
Link validation, when the original file is deleted | Still valid and file can be accessed. | Invalid |
Command used for creation | ln | ln -s |
inode number | Same | Different |
Can be linked | To its own partition. | To any other file system even networked. |
Memory consumption | Less | More |
Relative Path | Not applicable | Allowed |
Definition of Hard link
Hard links directly link two files in the same file system and for identification it uses file’s inode number. Hard links cannot be implemented on directories (since they point to inode). When “ln” command is utilized to generate a hard link, it creates another file on the command line which can be used to refer the original file. Both the original and generated file have the same inode and content; hence they will have the same permissions and the same owner.
The removal of the original file does not influence the hard linked file, and a hard linked file will remain. The inode holds a counter, to computes the number of hard links to itself. When the counter indicates 0 value, then the inode is emptied. Whenever you make any changes to the hard link, it will imitate in the original file.
Definition of Soft link
Soft links are usually an alternative path (or an alias) for the original file; these are also referred to as symbolic links. It includes the name of the link’s “target file”, flag which specifies that it is a soft link. When a file is accessed the soft link redirects it to the target file through the path written in the subject of the soft link.
These are very handy in case of windows OS where the soft link behaves as shortcuts. Creation and deletion of the soft links do not affect the original file. If the target file is deleted the soft link dangles which means it points nowhere and generates an error message when the target file is accessed. Soft links do not use inode number, unlike hard link. An absolute or a relative path could be a part of the symbolic links.
Key Differences Between Hard link and Soft link
- A hard link is an additional name of the original file which refers inode to access the target file. In contrast, soft link is distinct to the original file and is an alias to original file but do not uses inode.
- When an original file gets deleted soft link becomes invalid whereas, a hard link is valid even if the target file is deleted.
- In Linux, the command used for the creation of hard link is “ln“. As against, the command used for a soft link is “ln -s“.
- Hard link has same inode number dissimilar to soft link, where target file and its soft link have distinct inode number.
- Hard links are restricted to its own partitions, but soft links can cover different file systems.
- The performance of hard link is better than soft link in some cases.
- Relative path and absolute path both are allowed in soft links. On the contrary, the relative path is not allowed in a hard link.
Conclusion
A hard link doesn’t requires extra space and mat resolve faster, but the changes applied to a hard link reflect in the original file. On the other hand, Soft link needs extra space but any change in the soft link doesn’t impact the original file. Soft links are allowed to directories unlike to Hard link.
lego says
nice content
Shailaja Goagte says
Very good.
This is my favourite website for understanding any technical differences between the two concepts.
amr ahmed says
Thank you so much for this content as it’s simple and so clear
Sarfraz Haq says
Very informative and helpful site the author has done excellent job.
poggers says
true