Hard link definition
A hard link is a directory entry that associates a file name with a file in an operating system, allowing multiple file names to refer to the same file. Hard links point to the underlying file even if someone changes its name. Each file must have at least one hard link, but additional hard links can be created to make it accessible via other paths. Hard links allow files, scripts, and programs to be accessed in a different directory from the original file.
How hard links work
- Hard links are created by adding a new name for an existing file on the operating system. The original file and the new hard link have the same inode number, meaning they share the same data and metadata.
- Accessing the original file using a hard link means accessing the same physical data on the disk. The file system tracks the number of hard links pointing to the data to know when it is safe to delete.
- Changes made to the data in one file (original or hard link) are reflected in all the other linked files.
- Deleting a hard link will make the system reduce the link count for the data. When the link count drops to zero (meaning there are no more hard links pointing to the file), the system can safely delete the data from the disk.
Hard link vs. soft link
- Hard link. A hard link is a reference to an existing file on the system with the same data and metadata as the original.
- Soft link. A soft link is a separate file that contains a path to another file or directory on the system. It points to the original file rather than sharing the same data and metadata.