What is a hard and symbolic (soft) link in Linux or Unix?
A hard link is a directory entry that points to an inode, while a soft link or symbolic link is a directory entry that points to an inode that provides the name of another directory entry. The exact mechanism for storing the second name may depend on both the file system and the length of the name.
Symbolic links refer to:
A symbolic path indicating the abstract location of another file. It is like a shortcut in Microsoft Windows operating system.
Hard links refer to:
The specific location of physical data. It an essentially a label or name assigned to a file.
Hard link vs Soft link:
A Soft link:
- Can cross the file system
- Allows you to link between directories
- Has different inode number and file permissions than original file
- Permissions will not be updated
- Has only the path of the original file, not the contents
A Hard Link:
- Can’t cross the file system boundaries (i.e. A hardlink can only work on the same filesystem)
- Can’t link directories
- Has the same inode number and permissions of original file
- Permissions will be updated if we change the permissions of source file
- Has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.
Check the inodes and permissions of symbolic link:
Check the inodes and permissions of hard link:
How do I create symbolic link in Linux?
To create a symbolic link to a given file, open your terminal and type using
ln -s :
ln -s source_file my_link
ln -s /path/to/file1.txt /path/to/file2.txt
ls -s /etc/hosts /tmp/file
Above command will create a symbolic link to /etc/hosts as /tmp/file.
How do I create hard link in Linux?
To create a hard link use the ln command as follows:
ls [options] /path/to/source /path/to/link
## create a hard link ##
ln /home/vivek/my-awesome-long-file.txt /home/vivek/file
And that’s it!
Symbolic and hard links are very useful tools and are very easy to use.
I hope that this blog post helped you understand a little better the differences between the two types of links and how to use them.