What is the difference between a Hard link and a Symbolic link?

Santiago Zapata Bedoya
5 min readNov 16, 2020
Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

Do you know the shorcuts of microsoft and the security backups? Alright.. This is pretty much like that. So let’s explain it this way:

First we should know what does every file in Linux contains, and this is the inode. So let’s take a look to what an inode is:

Inode: The inode of a file is like an ID for each file, and as an ID contains a lot of information of ourselves, so does the inodes. Here are some things that an inode contains:

  • Inode number
  • File size
  • Owner information
  • Permissions
  • File type
  • Number of links
  • Among others..

If you want to see everything an inode contains you can see it here!

Now that we have that clear, let’s talk about hard link and symbolic link

  1. Hard link: A hard link is a file that references or points to a spot in the hard drive, which is the inode. So as they are pointing to the same spot, they must have the same inode number; and they also share the same size, but they can have different names. If you modify the hard link file, then the original file will be modified too, but if you delete the original file, nothing will happen to the hard link file.. This is why I say that it is almost like a backup, but with some differences.
  2. Symbolic (Or soft) link: A soft link is a file that points to a file, and this is the most important difference about symbolic and hard links; so because of this, we can notice that they do not point to memory, they just reference to a file, which is why we can say the following:
  • They have different inode number
  • The soft link file has smaller size than the soft-linked file (The original)
  • If you delete the original file, the soft link file will be useless. It will point to anything

This is why we can assure that a symbolic (or soft) link is basically like a shortcut in windows.

Now.. Let’s see how to create both of them

As we just saw the differences between hard link and soft link, let’s take a look to see how to create them on a Linux 64-bits machine:

Create a hard link

To create a hard link on Linux we must use the ‘ln’ command; this command is used exclusively to link files. Let’s take a look at it:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

Alright.. Just to make it as clear as possible let’s illustrate it with a complete example. In the image above we can see that there’s a file called ‘file1.txt’, and its content is a sentence: ‘This is a regular file’. Now let’s create the hard link file to this file:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

So as it was said before, we use the ‘ln’ command, followed by the name of the file to be hard-linked and last we put the name of the hard link, which in this case is ‘hardlink.txt’.

And that’s it. We have created our first hard link. If you type ls you can see it:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

And as we can see, they both have the same size: 23 bytes.

Now let’s modify the hard link.

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

As you can see, at the top of the nano editor, we are in the hardlink.txt file, and we changed the word ‘regular’ to ‘hardlinked’. Now let’s see if there’s any change in file1.txt:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

As we can see. There has been a change in the file. Now let’s remove file1.txt and check if it affects the hard link file:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

As we can see, hardlink.txt is still there, it didn’t seem to be affected by the deletion of the file1.txt file. That is because it points to the same spot in the hard drive memory (They have the same inode number).

Take a look at the symbolic link

To create a soft link, you use the same ‘ln’ command, only that you must use the -s flag (S for Soft or Symbolic). Check it out:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

As with the hard link, after the flag we put first the file that is going to be soft-linked and then the name of the soft link file. Again you can check it with the ‘ls’ command:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

Now this is a bit different from the hard link example, because the ‘ls’ command is telling us that softlink.txt is a soft link file that is pointing ‘->’ to the hardlink.txt file. And we can also tell that they have different size: hardlink.txt occupies 26 bytes and softlink.txt occupies12 bytes of memory.

If we see the content of the soft link, we can see that it’s exactly the same as the hardlink.txt file:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

The problem comes when we remove the hardlink.txt file:

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

Now the shell is warning an error, because softlink.txt is pointing to hardlink.txt but this last one doesn’t exist anymore.

Image source: https://www.youtube.com/watch?v=lW_V8oFxQgA

As we can see. Now softlink.txt is pointing to nowhere and has no data at all. That is because it points to files, not to data.

And this is basically the difference between hard links and symbolic links. Now that you know how do they work and how to create them, give it a try!

--

--