1. Introduction

In Linux, hard links are a powerful feature that allows multiple files to point to the same inode, or data block on the filesystem. While hard links are a useful tool for managing files, they aren’t allowed for directories.

In this tutorial, we’ll explore why hard links aren’t allowed for directories in Linux and how symbolic links can be used as an alternative.

To understand why hard links aren’t allowed for directories, it’s important to understand how hard links work. When we create a hard link, it creates a new file entry that points to the same inode as the original file. This means that any changes made to the original file will also affect the linked file and vice versa.

To illustrate this, let’s create a new file and a hard link to that file using the ln command:

$ echo "Hello, world!" > myfile.txt 
$ ln myfile.txt mylink.txt

Furthermore, if we modify myfile.txt, we see that the changes directly reflect in mylink.txt :

$ echo "Goodbye, world!" > myfile.txt 
$ cat mylink.txt 
Goodbye, world!

This behavior can be useful for managing files but can cause serious problems when applied to directories.

3. Circular Reference Situation

When we create a hard link and link it to a directory, it creates a new reference to the same directory inode. This can lead to a situation known as a circular reference. Basically, it’s where a directory contains a link to itself or its parent directory.

To clarify this, let’s create a new directory, mydir, and a hard link, mylinkdir, to that directory using the mkdir command:

$ mkdir mydir 
$ ln mydir mylinkdir

To begin with, listing the contents of mydir, we’ll see that it contains a link to itself. We’ll use the ls command to check the contents of mydir:

$ ls -l mydir 
total 0 
lrwxrwxrwx 1 user user 5 Apr 6 10:38 mylinkdir -> mydir/

Consequently, this circular reference can cause confusion for the file system. Resulting in difficulty in determining the actual location of files and directories. Moreover, It can also lead to unintended consequences, such as deleting a directory that contains a link to itself, which can cause the entire file system to become corrupted.

4. Difficult Subdirectory Management

In addition to circular referencing, one of the reasons why hard links aren’t allowed for directories in Linux is that subdirectories can be linked to different parts of the same hierarchy. In this section, we’re exploring how multiple linking of subdirectories within the same hierarchy can occur.

Let’s imagine a directory contains a hard link to itself or a subdirectory. It creates a situation where the same subdirectory links to different parts of the hierarchy. This can cause confusion for the file system, as it becomes difficult to determine which link is the correct one. Fundamentally, let’s create a new directory, mydir, and a hard link, mydir/mylinkdir, to a subdirectory, mydir/subdir, within that directory:

$ mkdir mydir
$ mkdir mydir/subdir
$ ln mydir/subdir mydir/mylinkdir

Basically, if we list the contents of mydir we’ll see that it contains a link to subdir:

$ ls -l mydir
total 0
drwxrwxr-x 2 user user 6 Apr  6 11:35 mylinkdir
drwxrwxr-x 2 user user 6 Apr  6 11:34 subdir

However, if we list the contents of mylinkdir, we’ll see that it’s actually a link to subdir:

$ ls -l mydir/mylinkdir
total 0
drwxrwxr-x 2 user user 6 Apr  6 11:34 .
drwxrwxr-x 3 user user 6 Apr  6 11:35 ..

This means that subdir is linked to two different parts of the same hierarchy, mydir and mydir/mylinkdir, which can cause confusion for the file system.

5. Difficult To Determine Parent Directory

Furthermore, in a directory containing a hard link to its parent directory, it can be difficult to determine the parent directory’s actual parent directory. This causes issues when navigating the file system. Making it difficult to determine the actual location of files and directories.

Similarly, let’s create a new directory, mydir, and a hard link to its parent directory:

$ mkdir mydir
$ ln mydir ..

By listing the contents of the parent directory, we see that it contains a link to mydir:

$ ls -l ..
total 0
drwxrwxr-x 2 user user 6 Apr  6 11:40 mydir
drwxr-xr-x 1 user user 6 Apr  6 11:34 otherdir
lrwxrwxrwx 1 user user 4 Apr  6 11:40 mylinkdir -> mydir

However, by listing the contents of mydir, we see that it contains a link to its parent directory:

$ ls -l mydir
total 0
lrwxrwxrwx 1 user user 2 Apr  6 11:40 .. -> ..

It can be difficult to determine the actual parent directory of mydir since it appears to be linked to itself, creating confusion.

Linux doesn’t allow hard links to directories to avoid these potential issues. We can use symbolic links, also known as soft links, as an alternative. In essence, symbolic links provide a way to create a shortcut to a directory without creating a new reference to the directory itself. In addition, symbolic links are safer and more flexible for referencing directories in Linux.

First things first, let’s create a symbolic link, mysymlink, to the mydir directory:

$ ln -s mydir mysymlinkdir

In order to create symbolic links, we use the ln command as well just as we did to create a hard link earlier. However, we use the -s option to indicate this is a soft link. Alternatively, if we omitted the -s option, we would’ve created another hard link.

Let’s get solid proof that when listing mysymlink, we get a link to mydir:

$ ls -l mysymlinkdir 
total 0 
lrwxrwxrwx 1 user user 5 Apr 6 10:44 mysymlinkdir -> mydir/

As we can see from the above example and contrary to hard links, symbolic links are actually a name-based reference to mydir and do not contain a link to itself resulting in a loop. 

Symbolic links are a safer alternative to hard links for referencing directories in Linux. In addition, they allow for flexible referencing of directories without creating a circular reference. Making it easier to manage files and directories on the filesystem.

7. Conclusion

In conclusion, we can say that Linux doesn’t allow hard links for directories due to the potential for circular referencing and other serious issues, despite their usefulness as a powerful feature.

On the other hand, symbolic links provide a safer and more flexible alternative for referencing directories in Linux. By understanding the differences between hard links and symbolic links, we can manage files and directories more effectively on the Linux filesystem.

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