Links in Linux are pointers that can be used to refer to files or folders. They come in handy in cases when we might need to access files deep into the file system or while linking libraries and much more! In this module, we are going to learn more about Links in Linux.
Types Of Links
There are basically two types of Links in Linux :
- Hard Links
- Soft Links
Both have their own significance and as we read on, we shall discuss each in details.
What Are Hard Links?
Hard Links in Linux can be considered as the link between filenames and the actual data stored in the filesystem. Creating a new hard link basically signifies creating a new link to the same data stored in the file system. Thus even if we have two different files with different names, they both refer to the same data.
At this point, you can delete the original file and still access it’s contents with the help of the hard link as it refers to the same data in the file system.
Creating And Deleting Hard Links
The syntax to create a hard link in Linux is :
$ ln [FILENAME] [LINK NAME]
For example, let’s create a Hard Link to a file as such :
$ ln File.txt HardLink.txt
To verify that they are indeed the same file you can compare their sha1sums to find that they are indeed the same. To “unlink” the file, simply delete the link. with :
$ rm [LINK NAME]
Hard Links vs Copies
At this point you might say: “So Hard Links are basically the Copies of the Original Files ?”
Well, that is untrue. While both the Hard Links and the Copies of a File will give you the same data, they are still not the same. Hard Links refer to the data in the SAME inode whereas copies store the same exact data in a DIFFERENT inode. To demonstrate this, we shall take the example from the earlier example and create a copy of the original file named Copy.txt and then check the inode numbers with ls -li :
$ ls -li
154 -rw-r--r-- 1 user user 1000 Mar 23 13:19 Copy.txt
153 -rw-r--r-- 2 user user 1000 Mar 23 13:12 File.txt
153 -rw-r--r-- 2 user user 1000 Mar 23 13:12 HardLink.txt
As you can see, both the original file and the Hard Link refer to the same inode number of 153 while the copy of the file has an inode number of 154.
Limitations of hard links in Linux
Hard Links have some shortcomings as well, for example – you cannot(or rather you should not) create Hard Links of directories and special files as it might break your File System. Also, they cannot span across multiple file systems and must reside on the same File System as the original file.
What Are Soft Links?
Soft Links in Linux, also known as Symbolic Links, point to a specific location(file/directory). Unlike Hard Links, they do not point to the inode data but rather point at the File itself.
Unlike Hard Links, deleting the original file “breaks” the soft link. A soft link can cross file systems, while allowing you to link between directories. A soft link can also have different permissions than the original file.
Creating And Deleting Soft Links
The syntax to create a soft link is :
$ ln -s [FILE/DIRECTORY] [LINK NAME]
For example, let’s create a Soft Link to a file with :
$ ln -s File.txt SoftLink.txt
You can create soft links to directories as well. When you create a soft link, doing an ‘ls -l’ should reflect the fact that it is a file as such :
$ ls -l
-rw-r--r-- 1 user user 1000 Mar 23 17:06 File.txt
lrwxrwxrwx 1 user user 8 Mar 23 17:08 SoftLink.txt -> File.txt
Note that the SoftLink has been pre-pended with the letter ‘l’ which signifies that it is a link rather than a file. Depending upon your terminal type, you might as well have the file displayed in a different color.
Note that renaming or deleting the original file, it will “break” the symbolic link which would still keep pointing to the old file.
$ ls -l
-rw-r--r-- 1 user user 1000 Mar 23 17:06 File-Renamed.txt
lrwxrwxrwx 1 user user 8 Mar 23 17:08 SoftLink.txt -> File.txt
$ file SoftLink.txt
SoftLink.txt: broken symbolic link to File.txt
To delete the soft link, simply remove it using the rm command. Likewise any changes made to the file linked using soft links shall be reflected on the main file as it is merely a pointer to the same.
Conclusion
Both links can be instrumental depending upon the situation. Links form an integral part of the Linux system and often help in navigating and exploring file system.