image take from TechMind.com

Hard or Symbolic link?

Thomas Sorza

--

Have you ever heard about a hard link or symbolic even soft links, but you don´t know how to use it or even which commands you required to create them? Well, here I go to explain to you what is this kind of links, how to create them, and some examples that will help you to understand the different uses that you can use when you are working in your Linux system.

  • let’s take a look at The Hard Link:

As you know a link is a reference to data that the user can follow by clicking or tapping, when you click on a link in your browser it will redirect you to another page or website, in the same way in Linux, we have an original file points to a specific spot on the hard drive, that is where it stores its data; and if you create a hard link, it would point to the same spot on the hard drive, As you can see in the picture.

To create a hard link we use the command (ln) with not complement or flags, like this:

ln [File_name] [Hard_link_name]

Let´s take a look at an example:

In this example, we have a file in the current working directory called file1.txt and the content of that file is: “This is a regular file” and we create a hard link to file1.txt called harlink.txt, both of them, the hardlink.txt and the file1.txt have the same permissions and occupy the same space in the hard drive, 23 bytes because both are pointing to the same spot on the hard drive.

And what happens if we edit the hard link content? Well, if we edit the hardlink.txt content to “This is a hard-linked file” and we look to the content of the file file1.txt we can see that is the same content that was edited in the hardlink.txt.

And finally, What do you think will happen to the hardlink.txt if you remove the file1.txt? It´s interesting because if you delete any file that has a hard-link the hard link doesn´t have to be affected; as we can see in the next image:

Well, I hope the concept of what is a Hard link is clear!

  • Now let´s talk about that is a symbolic link:

A symbolic link (also called Soft Link) is a link that points to a file while the file is pointing to a specific spot of data in the hard disk, to make it clearer, in the image we can see an original file pointing to a spot of data in the hard disk, and a soft link that it is pointing that original file.

To create a soft link we use the same command (ln) but now, with the dash (s) that means symbolic, just like this:

ln -s [File_name] [Symbolic_link_name]

Now let’s see an example of how to use a symbolic link:

In this example, we have a file called hardlink.txt that comes from the previous example. then we create a soft link of the file hardlink.txt called softlink.txt and we use the command ls -l to print all the directory content of the working directory, in this way:

We can appreciate that the softlink.txt link is pointing to the hardlink.txt file and the content of the softlink.txt is the same that the hardlink.txt, but the softlink.txt size is half of the original file hardlink.txt, only 12 bytes compare with 26 bytes because softlink.txt only pointing to hardlink.txt, not to a spot in the hard drive.

The problem comes when we try to remove the hardlink.txt, what will happen with the softlink.txt? that´s an amazing question, well let´s take a look at the next image:

Wow, What´s just happened? okay, let´s analyze the situation, remember that the softlink.txt is pointing to the hardlink.txt file, but we delete the hardlink.txt so the softlink.txt is pointing to something that doesn´t exist, that´s because we have the softlink.txt and the hardlink.txt highlighted in red and the content of the softlink.txt doesn’t exist, your OS can´t find anything to show.

In summary, I hope you are clearer about the concepts of what is a hard link and a symbolic link, know how to create them, and use them in your daily work. Remember that a hard link points to a point in the hard disk and the soft link points to another file, the hard link does not depend on the original file and the soft link depends on it.

See you next time.

Written by Thomas Sorza.

--

--

Thomas Sorza

System and Computation Engineering (Ingeniera de Sistemas y Computación) student, Linux administration, and networks passionate Loving the terminal and Python