Using Hard and Symbolic Links in Linux

Using Hard and Symbolic Links in Linux

Spread the love

Back when Linux was in earlier development, it used to utilize a “flat” directory structure. This meant that unlike the hierarchical “tree-link” folders we have today, everything was located inside of a single folder. No subdirectories existed. It also meant that each file had to have a unique filename, without exceptions!

All of that changed when Linux baked “hard links” into its kernel. The special character sequence “..” was defined to mean the parent directory, and it’s now the backbone of the modern Linux filesystem as we know it today. With that, we can see the importance of hard links – and in this tutorial, we’ll explain the difference between symbolic links and hard links, and the use-case scenarios, as well as when to use each type of link.

What Are Symbolic Links?

A symbolic link is a link to a specific file that gets treated by applications and programs as the file itself. It does not copy the file – it’s a pointer, pointing back to the original file instead. It doesn’t take up any extra space, and it can be moved around just like any other file.

It’s a convenient way to have a single file in multiple directories. Linux packages use this all the time – they commonly create symbolic links to their scripts in the global environment paths for the system. This makes it easy to execute these scripts without having to specify the full path.

The key concept around symbolic links is that there’s only one “real” copy of the file, despite it being mentioned in several locations. So if you make a change, that modification will propagate to all other symbolic links (and the original file) everywhere else.

We can create a symbolic link to a file by using the following syntax:

ln -s [originalfile] [symlinkoriginal]

This can be seen in this screenshot here:

Create Symbolic Link to File
Creating a Symbolic Link to a File

As you can see, the new file “symlinkoriginal” shows up with the “ls -l” command as a separate color. It also shows that it’s pointing to the first file.

Inode Counters

With the “ls -l” command, look at the second column in the previous screenshot. This is the number of pointers to the data, represented by the inode number. A regular file has a single pointer to the inode. With a symlink, that number doesn’t change because the symlink is pointing to the file and not the underlying data.

Deleting the Original File With a Symbolic Link

In the following screenshot, we’ll remove the original file with the symlink still pointing to it:

Data Gone if Original File Removed
The Data is Gone if the Original File is Removed

As you can see here, once the original file is gone, the symbolic link no longer works. When we try to read it, the system says “No such file or directory”. And when we list the file with “ls”, it shows up in red with an arrow pointing to the deleted file.

This shows that symbolic links are subservient to the actual file.

What are Hard Links?

A hard link, on the other hand, points to the actual inode containing the data instead of the original file. This has all sorts of implications. Firstly, a hard link is treated on an equal footing when compared to the original file name. The operating system can’t tell the difference.

Secondly, when you delete the original file, the hard link’s connection to the inode remains and the data can still be accessed unlike with symbolic links.

Creating a Hard Link

To create a hard link, use the following (without the “-s” option, which was used to create a symbolic link):

ln [originalfile] [symlinkoriginal]

It should look like this:

Create Hard Link to File
Creating a Hard Link to a File

This time after creating the hard link, we list the files again. Now, the hard link shows up as just a regular file, no different from any other. Also, note that the inode counter in the second column shows “2”, instead of “1”, which is unlike the symbolic link. This means that there are two pointers to the inode data – the original file, and the hard link.

Deleting the original file has no impact on the hard link:

Data Still There if Original File Removed
The Data is Still There, Even When the Original File is Removed

As you can see, once the original file has been deleted, the inode count is decreased by one, and all that’s left is the hard link. In a way, we have “moved” the file. In fact, this is why moving a file to another location is so much faster than copying it. The system creates a hard link in the new directory and deletes the original filename.

The uses of hard links are varied. Most of the time, symbolic links will be enough for most use cases. Using hard links can create confusion as to which file is the original and which isn’t, while symbolic links are clearly marked.

Occasionally, we’ll come across a package like ioquake3 for example, which doesn’t support symbolic links to pk3 files. However, these are likely bugs, and in these sorts of situations, we can fix the problem with hard links, since they’re the same as file names. 


Of course, you don’t have to do any of this if you use one of our Outsourced Server Support Services, in which case you can simply ask our expert Linux admins to set up symbolic links or hard links for you. Just sit back, relax, and let our admins take care of the issue for you. They are available 24×7 to help you with your requests.

PS. If you liked this post on using symbolic links and hard links, please share it with your friends on the social networks by using the share shortcut buttons, or simply leave a comment in the comments section below. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *