The links in Unix are the pointers which point 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 cross 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 permissions, owner of the file and more.
Content: Hard link Vs Soft link
|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|
|File accessing time||More ||Less|
|Can be linked||To its own partition.||To any other file system even networked.|
|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 (because they point to inode). When “ln” command is used to create 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.
Even if the original file is removed, the hard linked file will remain. The inode contains a counter, which counts the number of hard links to itself. When the counter decreases to zero, then the inode is emptied. Whenever you make any changes to the hard link, it will reflect in the original file.
Definition of Soft link
Soft links are usually an alias for the original file; these are also known as symbolic links. It contains 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 content 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. A soft link can contain either an absolute or a relative path.
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 unlike soft link, where target file and its soft link have different inode number.
- Hard links are limited to its own partitions, but soft links can cross file system boundaries.
- File accessing faster in case of hard link whereas soft links are slower and consume more space comparatively.
- Relative path and absolute path both are allowed in soft links. On the contrary, the relative path is not allowed in a hard link.
A hard link is faster and consumes less space, but the changes applied to a hard link reflect in the original file. On the other hand, Soft link is slower and consumes more space but any change in the soft link doesn’t impact the original file. Soft links are allowed to directories unlike to Hard link.