Hard and Symbolic links: Concepts, How to create them and Differences

Alex Rivera Cruz
5 min readFeb 3, 2021

--

In this article we will explain some interesting things that Gnu-Linux user should know about links or shortcuts in Linux.

The first thing we have to know is that in Linux there is no such thing as a direct access in Windows. In Linux the shortcuts are called links.

There are also two types of links, hard links and symbolic links. In the following sections we will explain what they are, how we can create them y what are the differences between each other.

Hard Links

Hard links associate two or more files sharing the same inode.

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 the following:

  • The permissions of the file or folder.
  • The owner of the file and folder.
  • The location of the file or folder on our hard drive.
  • The creation date of the file or directory.
  • Etc.

This makes each hard link an exact copy of the rest of the associated files and implies that when changes are made to one of the links or to the file, this will also be made to the rest of the links.

How to create a Hard Link

First we will create a text file by executing the following command in the terminal: touch lex.txt

Then, check its inode number by running the following command in terminal: ls -li lex.txt

You can see that the inode is 139673 and there is only 1 file or entry on the system that is pointing to the same inode.

Now, we will create our hard link to the file we just created lex.txt, calling this link hardlinklex, execute the following command: ln lex.txt hardlinklex

Created the link, we will check again the inode number executing the following command: ls -li lex.txt

We can see that the inode number didn’t change, however, now we have 2 files / entries pointing to the same inode. These 2 files / entries are the original file plus the hard link we just created.

So, both the hard link and the original file have the same permissions, the same owner and are part of the same group.

Symbolic or Soft Links

Symbolic link is similar to direct access in Windows and are the links that all common users use on a regular basis.

Each symbolic link has its own inode number which allows to make symbolic links between different file systems.

Keep in mind that in Linux / Unix if we delete the source file or directory, the symbolic link remains but the data disappears forever.

How to create a Symbolic Link

We are going to create a file called lex2.txt: touch lex2.txt

Then, check its inode number by running the following command in terminal: ls -li lex2.txt

We can appreciate that the inode number is 139675 and there is only 1 file or entry on the system that is pointing to the same inode.

Now, we will create our symbolic link to the file lex2.txt, calling this link softlinklex, execute the following command: ln -s lex2.txt softlinklex

Note that we aggregate the option -s, this is the part of the command that indicates that the type of link we want to create is a symbolic link.

Created the link, we will check again the inode number executing the following command: ls -li lex2.txt

As you can see, nothing changed, now, execute the following command: ls -li softlinklex

We see that the original file and the link we have created have a different inode.

Therefore they are not pointing to the same content, the original file is pointing to content stored on our hard drive, and the symbolic link is pointing to the original file name.

Differences between Hard and Symbolic Links

  • Hard links are exact copies of the file while symbolic links are pointers or “shortcuts”.
  • Hard links share the inode number, symbolic links don’t.
  • Symbolic links can be made with files and directories while hard links only between files.
  • Symbolic links can be made between different file systems, hard links cannot.
  • In symbolic links if the original file or directory is deleted, the information is lost, in hard links not

--

--