1. Overview

On a Linux machine, we can create links to an existing file. A link in Unix can be thought of as a pointer or a reference to a file. In other words, they’re more of a shortcut for accessing a file. We can create as many links as we want.

In this tutorial, we’ll quickly explore the two types of links: hard and symbolic links. We’ll further talk about the differences between them.

A file in any Unix-based operating system comprises data block(s) and an inode. The data blocks store the actual file contents. On the other hand, an inode stores file attributes (except the file name) and the disk block locations.

A hard link is just another file that points to the same underlying inode as the original file. Thus, it references the same physical file location.

We can use the ln command to create a hard link:

$ ls -l
-rw-rw-r-- 2 runner3 ubuntu 0 Sep 29 11:22 originalFile
$ ln originalFile sampleHardLink
$ ls -l
-rw-rw-r-- 2 runner3 ubuntu 0 Sep 29 11:22 originalFile
-rw-rw-r-- 2 runner3 ubuntu 0 Sep 29 11:22 sampleHardLink

The ln command creates hard links by default. In this case, we create a hard link (sampleHardLink) for the originalFile file.

Let’s quickly see their mapped inode numbers:

$ ls -i -1
2835126 originalFile
2835126 sampleHardLink

Both of these files point to the same inode. With this, even if we later delete the original file, we’ll still be able to access its contents using the created hard link. Effectively, once no hard link points to an inode, the file is considered deleted.

The -1 option in this command lists one file per line. In addition, we can use the cp command with the –link (-l) option to hard link files instead of copying.

However, we can’t create hard links for directories. Also, hard links cannot cross filesystem boundaries, like between network-mapped disks.

A symbolic or soft link is a new file that just stores the path of the original file and not its contents. A soft link won’t work after moving or deleting the original file.

Let’s now create a soft or symbolic link:

$ ln -s originalFile sampleSoftLink
$ ls -l
-rw-rw-r-- 1 runner1 ubuntu  0 Sep 29 12:16 originalFile
lrwxrwxrwx 1 runner1 ubuntu 12 Sep 29 12:16 sampleSoftLink -> originalFile

The -s option, shorthand for –symbolic, makes a symbolic link of the specified file. In our case, the sampleSoftLink file is a symbolic link of originalFile file.

Unlike hard links, a soft or symbolic link is a file with a different inode number than the original file:

$ ls -i -1
2835126 originalFile
2835217 sampleSoftLink

We can create a soft link to a directory. Further, soft links enable us to link files across various filesystems.

Moreover, we can create soft links with the cp -s construct, where -s is shorthand for –symbolic-link.

4. Differences

Now that we understand what soft links and hard links are, let’s quickly sum up the key differences:

  • hard links have the same inode number as the original file and can be thought of as its copy
  • soft links are new files that only store the file location of the original ones
  • hard links enable access to a moved or removed file
  • soft links become invalid once a file is moved or removed
  • unlike hard links, we can create soft links to directories
  • unlike hard links, soft links can span across filesystems

Thus, each type of link has its applications.

5. Conclusion

In this quick tutorial, we learned about the hard and symbolic links used in all Unix-based operating systems.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.