Hard Links vs. Symbolic Links — What Are They Used For?

Yago Martinez-Falero Hein
3 min readSep 14, 2020

In this article, we’re going to go over the main difference between hard links vs. symbolic links and see how both can be used.

Let’s talk about iNodes first

In order to understand hard and symbolic links, it is key to understand what an iNode is.

iNodes are data structures that contain information about files in Unix file systems that are created when a file system is created.

Each file has an iNode and is identified by an iNode number (i-number) in the file system where it resides:

iNode numbers on the left-hand side

What is a hard link?

A hard link is merely an additional name for an existing file on Linux or other Unix-like operating systems.

Any number of hard links, and thus any number of names, can be created for any file. Hard links can also be created to other hard links. However, they cannot be created for directories, and they cannot cross filesystem boundaries or span across partitionsSource

What does this mean?

Above we have the theory but let’s dive into what happens when we create a hard link.

We will create a hard link h_link1 of file1 by executing the following command: ln file1 h_link1 .

Now let’s list all the files with iNodes using ls -i . Here’s what we can see:

As you can see, both the file and the hard link have the same iNode. In short, hard links act as a shortcut to that file that is hard-linked.

How are hard links used?

Perhaps the most useful application for hard links is to allow files, programs, and scripts (i.e. short programs) to be easily accessed in a different directory from the original file or executable file (i.e., the ready-to-run version of a program). Typing the name of the hard link will cause the program or script to be executed in the same way as using its original name. — Source

What is a symbolic link?

Soft links is a special kind of file that points to another file, much like a shortcut. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. — Source

What does this mean?

Let’s illustrate this definition once again by creating an actual symbolic link.

We will create a symbolic link s_link of file2 by executing the following command: ln -s file2 s_link and then list the files with iNodes using ls -i :

As you can see, the symbolic link has a different iNode has its symbolic link file.

How are symbolic links used?

This difference gives symbolic links certain qualities that hard links do not have, such as the ability to link to directories, or to files on remote computers networked through NFS. Also, when you delete a target file, symbolic links to that file become unusable, whereas hard links preserve the contents of the file. — Source

--

--

Yago Martinez-Falero Hein

👨🏼‍💻 Entrepreneur and software engeneer // Former employee at TheFamily.co // 👨🏼‍🎓 Holberton School & Reverse Origins