Hard versus symbolic links

One of the most confusing topics for me when I started learning bash was the difference between a “hard” link and a “soft” (or “symbolic”) link. I knew that both were essentially pointers to a block of text, but what are each of them used for, and what are the real differences between them?

A “hard” link (for Unix-based operating systems) is simply another name for any currently existing file’s inode (which we’ll explore in a second). Notice the specification of “file” here — hard links are unable to refer to directories. They also cannot refer to files outside of the current filesystem (such as a file on a remote server) — they are unable to even refer to a file on a different partition of the same hard drive.

An inode, by the way, is the actual data represented by a file. You could think of the address of any file on a Unix-based system as being the first “hard link” (which I put in quotations intentionally) assigned to the inode.

Despite these limitations, hard links still have their uses. Perhaps the most applicable use is running executable files. One could, in their shell, run the “ls” command. This command, as well as all others, are, in fact executables themselves. However, they are hard-linked natively in bash, which allows them to be referred to by their names. “ls”, for instance, is found in the /bin folder, but is hard linked for ease of use.

One thing to note about hard links is that the assigned name for the linked file does indeed refer to that file’s inode, so if any command is entered that would remove the hard link, the original file (as well as its hard link association) would be removed.

Soft links are much different, despite the similar name. Rather than just acting as a surrogate name, soft links are themselves files. However, the only purpose of a soft link file is to point to another location. Soft links, since they are only pointers and not renames, do not preserve the contents/inode of the file they refer to. This difference leads to some interesting uses for soft links. Pointing to files on other systems is one such use, as well as linking to directories, which hard links are unable to do.

Soft links are also interesting in that the target file of said link can in fact be deleted without the link itself being destroyed. While the link becomes useless, replacing the linked-to file with one of the same name will cause the soft link to connect to the new file instead.

Like what you read? Give Jack Gindi a round of applause.

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