What is the difference between a hard link and a soft link (symlink)?
sobes.tech AI
Answer from AI
A hard link is an additional reference to the same inode of a file in the filesystem. When a hard link is created, another name for the file is generated, pointing to the same physical file on the disk. If one hard link is deleted, the file remains accessible through other hard links until the last link is removed. Hard links cannot be created for directories or files on different filesystems.
A soft link (symbolic link, symlink) is a separate file that contains the path to another file or directory. A symlink is a kind of shortcut. If the original file is deleted, the symlink becomes "broken" and does not work. Symlinks can point to files and directories, as well as objects in other filesystems.
Key differences:
- Hard link points to an inode, symlink is a separate file with a path.
- Hard links cannot be created for directories or across different filesystems, symlinks can.
- When the original file is deleted, the hard link retains the data, the symlink does not.
Example of creation:
ln original.txt hardlink.txt # hard link
ln -s original.txt symlink.txt # soft link