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

First of all we need to know that in Linux there is no what is called direct access in Windows. In Linux shortcuts are called links. We have two types of links in Linux, hard links and symbolic links. Here we are going to understand what is each of those links, and for what we can use each of the types of links that we have just mentioned.

Hard Links:

In Linux each file and each folder of the operating system have an integer called inode assigned to them. This inode is unique for each of the files and each of the folders. The information stored by each of the inodes of the different files and folders is as follows:

  • The file or folder permissions.
  • The owner of the file and folder.
  • The path of the file or folder in our hard drive.
  • The date when we create it.

In that order, a hard link is a file that points to the same content stored on disk as the original file.

To understand better what is exactly a hard link lists going to create one in the console. After create it, lets gonna check the inode number by executing the following command in the terminal ls -li, that looks like this:

Checking the inode Number.

Therefore the inode of the file we just created is 141488. We also see that currently there is only 1 file / entry in the system that is pointing to the same inode. Now we are going to create the hard link:

Creating the hard link.

Understanding the command:

  • ln → command in charge of making links between files.
  • enlaceduro.txt →It is the path or name of the original file that we have on our hard drive.
  • enlaceenalceduro →Corresponds to the path or name of the hard link that we are going to create.

After create the hard link, we are going to check again the inodo number of the original file, like this:

Checking inodo number of the original file.

As you can see the inod number is the same as before, but we have two files / entries pointing to the same inode. Now we are going to check the inodo number of the hard link we have created:

Checking the inodo numbre of the hard link.

Therefore, we can apreciate that the file and the hard link we have created have the same inode number, and point to the same information that is stored on our hard drive.

Symbolic Link:

Symbolic links are similar to shortcuts in Windows and are the links that all common users tend to use on a regular basis.

Unlike hard links, the symbols point to the name of a file and later the file points to content stored on our hard drive. In addition, each symbolic link has it own inode number and is different from the original file.

To understand it better, we are going to repeat the steps for a hard link. So first we create a file and we check it´s inodo number:

Creating a file and checkint inodo number.

Therefore the inode of the file we just created is 141489. We also see that currently there is only 1 file / entry in the system that is pointing to the same inode. Now we are going to create the hard link:

Now we create a symbolic link to the file we just created, and check the inodo number the symbolic link has:

Checking inodo numbre of the symbolic link.

As you can see, the inodo number of the file and the symbolic link are differents. This is due they are not pointing to the same content. Enlacesimbolico.txt is pointing to content stored on our hard drive, and the symbolic link is pointing to the name of the original file.

Summary:

  • Symbolic links can be made with files and directories while hard links can only be done between files.
  • Symbolic links can be made between different file systems, hard ones cannot.
  • Hard links share the inode number, symbolic ones do not. In symbolic links if the original file or directory is deleted, the information is lost, in hard ones it is not.
  • Hard links are exact copies of the file while symbolic links are mere pointers or “shortcuts”. “ taken from https://www.raulprietofernandez.net/blog/gnu-linux/como-crear-enlaces-simbolicos-y-duros-en-gnu-linux

The next image ilustrate the difference between hard and symbolic link

Image taken from https://rm-rf.es/diferencias-entre-soft-symbolic-y-hard-links/

If you want to understand more about this topic, you can visit the next web sites:

Hi I am a financial engineer, and a software development student in Holberton School!