The main difference between hard and soft links is that each hard-linked file is assigned the same Inode value as the original so that they refer to the same physical location while each soft linked file contains a separate Inode value that points to the original file.
In UNIX, a link is a pointer to a file. Therefore, a link points to a file or a directory. Creating links is a kind of shortcut to access a file. Links allow more than one file name to refer to the same file elsewhere. Generally, there are two types of links as hard links and soft links.
Key Areas Covered
Hard Links, Inode, Pointer, Soft Links, UNIX
What are Hard Links
Each hard-linked file is assigned the same Inode value as the original. Inode refers to a file structure on a file system. Therefore, they refer to the same physical file location. Generally, hard links are more flexible. The links remain the same even if the original or linked files are moved to some other location of the file system. However, it is not possible to move to different file systems.
These links have the actual file contents. If the user removes the links, it does not minimize the link count. It will only affect other links. Even though the original file is removed, the link will still display the content of the file. Furthermore, it is not possible to create a hard link for a directory to avoid recursive loops.
What are Soft Links
Each soft linked file contains a separate Inode value that points to the original file. It is possible to link the soft links across different file systems. If the user deletes or moves the original file, then the soft linked file will not work properly. The link always points to the original file. In other words, a soft link has the path for the original file but not the contents.
If the user removes the soft link, it does not affect anything. However, removing the original file will make the link become a “dangling” link. It will point to a nonexistent file. Moreover, a soft link can link to a directory. Additionally, if the user wants to link files across the file systems, then he can use only these soft links.
Difference Between Hard Links and Soft Links
A hard link is a directory entry that associates a name with a file on a file system. In contrast, a soft link is a term for any file that contains a reference to another file or directory in the form of an absolute or relative path that affects the pathname resolution.
Hard links have actual file contents whereas soft links contain the path for the original file and not the contents.
Command to create hard links is $ ln [original filename] [link name] while command to create sort links is $ ln -s [original filename] [link name].
Effect on removal
Removing the hard link will show the content of the original file while in soft links, removing the original file will make the link to become a dangling link which points to a nonexistent file.
It is not possible to link the files across different file systems using hard links. On the other hand, it is possible to link the files across the file systems using soft links.
In brief, the main difference between hard and soft links is that each hard-linked file is assigned the same Inode value as the original so they refer to the same physical location while each soft linked file contains a separate Inode value that points to the original file.
1.” Simplified illustration of hard links” By Mtodorov 69 – On Athlon Phenom x 4; Ubuntu 32-bit; LibreOffice Draw; export to PNG.Previously published: This work had not yet been published (CC BY-SA 4.0) via Commons Wikimedia