What is the Difference Between a Hard Link and a Symbolic Link?

Hello there again. In this introduction to hard links and symbolic links, we will first try to learn and define what each one means. Then we will discuss what the differences between the two are.

Hard Link Definition:

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 partitions.

Perhaps the most useful application for hard links is to allow files, programs, and scripts to be easily accessed in a different directory from the original file. Typing the name of the hard link will cause the program or script to be executed the same way as using its original name.

Creating a hard link example:

Hard link example

Now let’s run ls -i to list all files with their inode numbers.

INODE Definition:

The inode is a data structure in a Unix-style file system describing a filesystem object such as a file or directory. Each inode stores metadata and the disk block locations of the object’s data.

inode example

If you look at file1 and hlink1, they share the same inode. Hard links are literally additional directory entries pointing to the same underlying inode.

Soft Links (Symbolic Links)

A symbolic (soft) link is a special kind of file that points to another file, similar to a shortcut. Unlike hard links, symbolic links do not contain the data of the target file — they simply reference another path in the filesystem.

Soft links:

Creating a soft link example:

Soft link example

Now running ls -i:

inode soft link

Notice that only the hardlink file shares the same inode as the original file. Soft links always have different inodes.

Summary: Differences Between Hard and Soft Links

In essence:

Underneath the file system, files are represented by inodes. A file in the file system is basically a link to an inode. A hard link creates another name pointing to the same inode. The inode is only deleted when the last link to it is removed. A symbolic link is a link to a file name, not to the inode. Changing or deleting the original file breaks the symbolic link.

Examining the Shell and How It Executes a Command

shell execution

Original publication on Medium: Click here