Definition
A hard link is a directory entry that links a file name to a specific file on a disk, enabling multiple file names to refer to the same file.
It directs to the underlying file even if its name has been changed. A file can have at least one hard link, but you can create more to access the file through other paths.
Hard links facilitate the accessibility of files, scripts, and programs in a different directory from the original file.
How Hard Links Work
- You can create hard links by adding a new name for an existing file on the disk. The original file and new hard link share the same inode number, indicating identical data and metadata.
- Using a hard link to access the original file means accessing similar physical data on the disk. The file system monitors the number of hard links directing to the data to determine when it can be deleted safely.
- Any alteration to the data in one file (original or hard link) reflects on all the other linked files.
- Removing a hard link triggers the system to minimize the link count for the data. If the link count drops to zero, the system safely removes the data from the disk.
Difference Between Hard Link and Soft Link
A hard link points to an existing file on the system with data and metadata similar to the original. On the other hand, a soft link is a distinct file with a path to another file or directory on the system. It references the original file instead of sharing similar data and metadata.