What is the difference between a hard link and a symbolic link?
If you ask yourself this question, the answer is pretty straight forward, a hard link is essentially a synced carbon copy of a file that refers directly to the inode of a file. Symbolic links on the other hand refer directly to the file which refers to the inode. So.. what's the inode.
What is an inode?
The inode is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive. With an inode, the OS can retrieve information about the file such as permission privileges and the physical location of the data on the hard drive to access the file. Should a file be moved from one folder to another, the file will be moved to a different location on the hard drive and its inode value will change with it automatically.
So what's the difference between symbolic and hard link?
A hard link is a direct reference to a file via its inode. You can also only hardlink files and not directories. By using a hardlink, you can change the original file’s contents or location and the hardlink will still point to the original file because its inode is still pointing to that file, on the other hand, symbolic links are essentially shortcuts that reference to a file instead of its inode value. This method can be applied to directories and can reference across different hard disks/volumes. Since the symbolic link is referring to the original file and not its inode value, then replacing the original file into a different folder will break the symbolic link, or create a dangling link.
Differences in a nutchell
Hard links:
- Indistinguishable from other directory entries, because every directory entry is hard link
- "Original" can be moved or deleted without breaking other hard links to the same inode
- Only possible within the same filesystem
- Permissions must be the same as those on the "original"
- Can only be made to files, not directories
Symbolic links
- Simply records that point to another file path.
- Will break if original is moved or deleted.
- Can point to a file in a different filesystem
- Can point to a directory