Noobies often come to the point when they hear symbolic or soft links and hard links that; how do we create a hard link in Linux, UNIX, or macOS.
Hard Link is a way to create a link that points to file data in the filesystem by assigning a common inode value to source file data and the hard link. There is another type of reference linking known as a soft link.
How to Create Symbolic or Soft Link in Linux
Hard Link and Soft Link difference
The significant difference between a hard link and a soft link is that in the background when we create a hard link, it points to the actual file data in the filesystem via inode. In contrast, a soft link points to the filename of the file—a hard link created within a single filesystem. Soft links can be created to multiple filesystems.
When do you use Hard Link and Soft Link
Once we figure it out, the next thing is how do you know whether to use a hard link or a symbolic link in Linux. The simple difference is a hard link can still be accessed even if its source file is deleted. While soft link source file is deleted, then they are useless.
Creating Hard Link for Files
A hard link can be easily created with the help of a built-in command
ln available in almost all major UNIX and Linux distributions like Ubuntu, Kali, Parrot, Linux mint, etc.
$ ln [/path/to/file_or_directory] [path/to/hardlink]
I already created a sample file with name file.txt. Let us create a hard link for that file in the exact location using ln the command.
$ ln file.txt hardlink.txt
Here the source file is file.txt, and the new hard link file name is hardlink.txt. To check whether it is a hard link, we can check the inode number for both files using
ls-il the command and find them shared.
$ ls -il
Any changes or updates made in the source file or hard link file will directly reflect the source file.
Creating Hard Link for Directories
There is no option to create a hard link for a directory, which is strongly allowed due to its nature. A hard linking directory may break your file system structure. That’s why directories are not allowed to create a hard link. Go with a soft link in this case file point to the filename of the directory instead of actual data.
Removing Hard Link
Removing a hard link is easy, like a regular file using the
rm command. To remove, navigate to the exact location where you created your hard link and use the
rm command along with the hard link file name to delete.
$ rm hardlink.txt
Note: Hard link files can still be accessed even if their source file is removed to remove the file altogether, remove all hard links along with the actual source file.
Hard Link Limitations
Due to their nature pointing toward actual source files, it is hard to use command such as list files using the
ls command. Especially when you list files recursively, that’s why creating hard links is not allowed for the directory. And another limitation is you can only use hard links with a single file system. On the other hand, symbolic or soft links can be used in a multi-file system.
Let us know in the comment section do you use the hard link before or stuck with the soft link. If you find it informative, the comments section is always open to sharing your thoughts and feedback.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.