More that one link? WHAT?

What is the difference between a hard link and a symbolic link?

Checha Giudice
5 min readSep 19, 2020
Where to go?

No, I am not talking about about links as in “the red thread”, or “the missing link” in Monsters vs Aliens (great film, FYI). No, my dear reader, this post will be about what are links to your old realiable computer (well, sort of).

Imagine your computer as a filing cabinet, and it’s full with folders and files, and those folders have more folders inside with more files, and so on, and on and on… a pretty big filing cabinet, right? Well, that’s what your terminal is actually doing with your own files and folders.

But, what are links? Do they work the same as web links? Do I touch a button that reads “I’m feeling lucky”, and hope it gets me where I want to go, or do what I want it to do?

This “I’m feeling lucky” button.

Extreme case but stay with me on this. Links to the terminal work similar to web links on the Internet, but instead its final destination are files, not web pages. Now you know how “plain” links work. They get you to the “thing” you want.

Hard links and symbolic links?

Let’s get technical (finally!)

Underneath your file cabinet (your file system), files are represented by inodes. A file in the file system is basically a link to an inode. And an inode is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive. This inode will get you pertinent info about your file, such as permission privileges and the real “address” of the data to access the file on the hard drive.

Hard links and symbolic links are two different methods to refer to a file in the hard drive.

A hard link, then, just creates another file with a link to the same underlying inode. A symbolic link is a link to another name in the file system.

Elaborate, please!

OK, chill! A hard link is a direct reference to a file via its inode. You can also only hardlink files and not directories. Once a hard link has been made the link is to the inode. Deleting, renaming, or moving the original file will not affect the hard link as it links to the underlying inode. Any changes to the data on the inode is reflected in all files that refer to that inode.

A symbolic link (or softlink) is a type of file in Linux that points to another file or a folder on your computer. Soft links are similar to shortcuts, and can point to another file or directory in any file system. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. Also, when you delete a target file, symbolic links to that file become unusable, whereas hard links preserve the contents of the file.

Note: Hard links are only valid within the same File System. Symbolic links can span file systems as they are simply the name of another file.

I want to visualize it. Can you illustrate?

Of course, my dear reader! Let’s create both: a hard link and a symbolic link. Open your linux terminal, let’s program!

First, let’s create two files: fileH and fileS, and enter some data into them:

$ echo "Cat" > fileH
$ echo "Dog" > fileS

If you check your new files with ls -l (list files in long format) and cat (concatenate files and print on the standard output), your linux console should look like this:

For this blog I created a directory called “example”

Now, which commands actually create the links? ln (make links between files) is the correct one. So, ln to create a hard link with the file name to which we want the hard link to “point” and a new file name for the hard link; and ln -s to create a softlink with the file name to which we want the hard link to “point” and a new file name for the soft link:

$ ln fileH fileH-hard
$ ln -s fileS fileS-soft

Re-checking your linux console should look like this:

What happends if I change the name of the file, but not the hardlink’s?

fileH-hard points to the inode, the contents, of the file — that wasn’t changed.

What about the softlink?

The contents of the file could not be found because the soft link fileS-soft points to the name fileS, that was changed to fileS-new, so the cat command can’t display the contents of the file fileS-new because this doesn’t exist.

Likewise, If fileH is deleted, fileH-hard still holds the contents; if fileS is deleted, fileS-soft is just a link to a non-existing file.

And that’s it!

Hope you understood hard link and softlink, and enjoyed the reading!

--

--