Some Linux Links, please.

photo credit:

You walk into a shop named Linux.

“I’d like to order some links, please,” you said.

“Sure. Hard or soft?”

“What’s the difference?” you asked.

Allow me to explain.

In Linux, a link is a connection between two files. A soft link, or a symbolic link, functions much like a shortcut on a Windows desktop: it points and directs the user to the source file. We can create a symbolic link to a file by using the command syntax ln -s source_file target_file, where the source file is the file you would like the symbolic link to point to. For example:

If we ask the shell to list the files with the ls command, we can visually see what file the symbolic link is pointing to:

In this case, the file SoftLinkName is a symbolic link that points to the source file test.

A hard link, on the other hand, is just like a different name for the same file. We can create a hard link by using the command syntax ln source_file target_file. For example:

If we ask the shell to list the files in the long format, we can immediately notice the difference between the two links. Whereas the symbolic link contains an arrow that points to the source file (like a shortcut), the hard link shows up as two independent files.

In fact, if we try deleting the source file in the hard link, the other file remains unaffected, and the user can still access the same file content through the hard link. The symbolic link, on the other hand, appears broken (in red):

To understand why this difference exists between symbolic and hard links, it’s important to discuss the concept of inode in filesystem. Just as a file stores data, an inode stores information about the file. This information includes file modification times, ownership, permission, etc. We can use the command ls -li to view the inodes associated with each file (shown as the number listed on the first column):

Notice the inodes for test (268583)and the SoftLinkName (268576) are different, while the inodes for the source file and the HardLinkName are identical. In other words, both the hard link and the source file point directly to the same inode, suggesting they are, in fact, different names of the same file. Symbolic links, on the other hand, are files that have their own inode information, and are linked to the source file by name only.

Another way to verify the above information about symbolic and hard links is by looking at the number of links a file has, shown in Column 3. While the symbolic link has only 1 link (to the source file), the hard link has 2 links: one to the source file, and the other to the inode. The source file test also has 2 links, one pointing to the hard link and the other to the inode. Any modifications and changes made to the content of the source file will be reflected on the hard link, as well we changes to file permissions and other information, since hard links are essentially different names for the same file.

The visual below provides a nice illustration:

photo credit:

It is also worth noting that symbolic links appear more versatile, as it can link files as well as directories, and can do so across the filesystem. Hard links, in contrast, are for files only within the same file system.

Like what you read? Give Tanya Kryukova a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.