Eduardo Herrera Forero
5 min readFeb 2, 2021

--

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

This blog will explain differences between a hard link and a symbolic link

A hard link vs a Symbolic link

Many people who are new to Linux wonder how to create what in Windows is called a shortcut to be able to access quickly and conveniently to the most commonly used programs or locations. In order to solve this doubt, in this article we will explain everything that a Gnu-Linux user should know about the links or shortcuts in Linux.

DO SHORTCUTS EXIST IN LINUX?

The first thing to know is that Linux does not have what Windows calls shortcuts. In Linux shortcuts are called links.

In addition there are two types of links, hard links and symbolic links. In the following sections we will explain and we will see in detail what they are and what we can use each of the types of links that we have just mentioned.

Definition of hard link

Physical links directly link two files on the same file system and, for identification, use the inode number of the file. Physical links cannot be implemented in directories (since they point to inode). When the “ ln “ command is used to generate a fixed link, it creates another file on the command line that can be used to refer to the original file. Both the original file and the generated file have the same inode and contents; therefore, they will have the same permissions and the same owner.

The deletion of the original file does not influence the linked file, and a linked file will be maintained. The inode has a counter, to calculate the number of hard links to itself. When the counter indicates a value of 0, the inode is emptied. Whenever you make changes to the hard link, it will be mimicked in the original file.

Definition of soft link

Soft links are usually an alternate path (or alias) for the original file; these are also known as symbolic links . It includes the name of the “target file” of the link, a marking that specifies that it is a soft link. When a file is accessed, the soft link redirects to the target file via the path written in the subject of the soft link.

These are very useful in the case of the Windows operating system, where the soft link behaves like shortcuts. Creating and deleting soft links do not affect the original file. If the target file is deleted, the soft link hangs, meaning it does not point anywhere and generates an error message when the target file is accessed. Soft links do not use an inode number, unlike a hard link. An absolute or relative path could be a part of symbolic links.

Creating a hard link
The generic syntax for creating a hard link is as follows:

ln TARGET LINK_NAME

TARGET: Name of the existing file to which we will create the hard link.
LINK_NAME: Name of the hard link.

Here is an example:

$ ln test.txt hard_link_test.txt

If we list both files with the command ls -li, we observe that both files share the same inode:

$ ls -li
786433 -rw-r--r-- 2 edu edu 0 jun 21 21:27 hard-link-test.txt
786433 -rw-r--r-- 2 edu edu 0 jun 21 21:27 test.txt

Note in the first column that both file and link share the same inode number (786433). The third column indicates how many hard links the file has, in this case 2, the original file plus the link.

If we modify one of them, the changes affect all of them. For example, let’s grant execute permission to the owner on the file test.txt and see what happens to the link:

$ chmod u+x test.txt

If we list both files again we see that the change has affected both the original file and the link:

$ ls -li
786433 -rwxr--r-- 2 edu edu 0 jun 21 21:27 hard-link-test.txt
786433 -rwxr--r-- 2 edu edu 0 jun 21 21:27 test.txt

If we were to edit the file or the link, the changes made to the content would affect both.

Creating a soft link or a symbolic link

The generic syntax for creating a symbolic link is as follows:

$ ln -s TARGET LINK_NAME

s: Option to create symbolic links.
TARGET: Name of the existing file to which we will create the symbolic link.
LINK_NAME: Name of the symbolic link.

Here is an example:

$ ln -s test.txt sym-link-a-test.txt

If we list both files with the command ls -l, in the first column we can see that the link has at the beginning an l that identifies it as a link:

$ ls -l
lrwxrwxrwx 1 edu edu 8 jun 21 19:28 sym-link-a-test.txt -> test.txt
-rw-r--r-- 1 edu edu 0 jun 21 19:28 test.txt

We can check that the symbolic link has a different inode with the command ls -li:

$ ls -li
26547710 lrwxrwxrwx 1 edu edu 8 jun 21 19:28 enlace-simbolico-a-test.txt -> test.txt
26545137 -rw-r--r-- 1 edu edu 0 jun 21 19:28 test.txt

The first column shows the identifier numbers of its corresponding inode.

Key differences between hard and soft linking

  • A hard link is an additional name of the original file that references inode to access the target file. In contrast, the soft link is distinct from the original file and is an alias of the original file but does not use inode.
  • When an original file is deleted, the flexible link is invalidated, whereas a physical link is valid even if the target file is deleted.
  • In Linux, the command used for hard link creation is “ ln “. In contrast, the command used for a soft link is “ln -s”.
    - hard links:
    $ ln test.txt hard_link_test.txt
    - symbolic links:
    $ ln -s TARGET LINK_NAME
  • The physical link has the same inode number different from the flexible link, where the target file and its flexible link have a different inode number.
  • Physical links are restricted to their own partitions, but flexible links can cover different file systems.
  • The performance of the hard link is better than the flexible link in some cases.
  • Relative path and absolute path are allowed in soft links. In contrast, relative path is not allowed in a hard link.

In summary, a fixed link does not require additional space and the mesh resolves faster, but changes applied to a hard link are reflected in the original file. On the other hand, the soft link needs additional space, but any changes to the soft link do not affect the original file. Soft links are allowed to directories unlike the hard link.

--

--