Understanding Hard and Symbolic Links in Linux
Hello there!
Here I’m going to give a basic explanation of the hard link vs soft or symbolic link concepts, as you know the links are ‘connections’, but there are different kind of connections as we are about to see.
In UNIX the files are stored in two places, the data block and something called the inode, so first we must understand what it means. The inode is where the block location and other information about the file is stored, it means, the inode is a ‘database’ of all file information except the file content and the name, the inode links a filename to the file data in the block.
Symbolic vs Hard
Symbolic or Soft links are like shortcuts, it means, it links or connects files that are in different inodes.
Hard links, on the other hand, is like a copy of the original file, but different from a regular copy, because when you hard link a file, it shares the same inode that the other. If you delete the original file, the hard link will keep the data.
Here is a way to see it:
Filename-hard link > inode 1 > File Data
Filename-symbolic link > inode 2 > Link Data — — Filename
Filename > inode 1 >File Data
HARD LINKS:
- Can’t link directories.
- Have de same inodes.
- If the original file is deleted the hard link won’t change, because it has the original file content.
- If permissions are updated in the original file, the hard link permissions will be updated.
To create a hard link:
ln <source> <linkname>
Soft Links
- Can link directories.
- Have different inodes numbers and file permissions than the original file.
- If the original file change permissions, the soft link won’t.
- Don’t have the original file content, so if the original file is deleted, the link won’t work.
To create a soft link:
ln -s <source> <linkname>