Skip to main content

Hard links and soft links in Linux explained

Let's discuss when you should use hard links or soft links, and the syntax of the ln command.
Image
Hard links and soft links explained
Image by Dawid Śliwka from Pixabay

Have you ever been familiar with something, worked around it, but not fully understood its concepts? I feel like that happens to me more than most people. This is a frustrating feeling, but it is also one that often is easily remedied. Sometimes, it only takes someone explaining the concept in "plain English," aka layman's terms. That is the goal of this article. I want to talk about hard links and soft (symbolic) links in the most basic terms possible. You may realize that this concept, which is often a struggle for sysadmins, is quite simple. If nothing else, I take you through the syntax to create these links (which many people find difficult to remember). So let's get down to it.

Hard links

The concept of a hard link is the most basic we will discuss today. Every file on the Linux filesystem starts with a single hard link. The link is between the filename and the actual data stored on the filesystem. Creating an additional hard link to a file means a few different things. Let's discuss these.

First, you create a new filename pointing to the exact same data as the old filename. This means that the two filenames, though different, point to identical data. For example, if I create file /home/tcarrigan/demo/link_test and write hello world in the file, I have a single hard link between the file name link_test and the file content hello world.

[tcarrigan@server demo]$ ls -l
total 4
-rw-rw-r--. 1 tcarrigan tcarrigan 12 Aug 29 14:27 link_test

Take note of the link count here (1).

Next, I create a new hard link in /tmp to the exact same file using the following command:

[tcarrigan@server demo]$ ln link_test /tmp/link_new

The syntax is ln (original file path) (new file path).

Now when I look at my filesystem, I see both hard links.

[tcarrigan@server demo]$ ls -l link_test /tmp/link_new 
-rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 link_test
-rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 /tmp/link_new

The primary difference here is the filename. The link count has also been changed (2). Most notably, if I cat the new file's contents, it displays the original data.

[tcarrigan@server demo]$ cat /tmp/link_new 
hello world

When changes are made to one filename, the other reflects those changes. The permissions, link count, ownership, timestamps, and file content are the exact same. If the original file is deleted, the data still exists under the secondary hard link. The data is only removed from your drive when all links to the data have been removed. If you find two files with identical properties but are unsure if they are hard-linked, use the ls -i command to view the inode number. Files that are hard-linked together share the same inode number.

[tcarrigan@server demo]$ ls -li link_test /tmp/link_new 
2730074 -rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 link_test
2730074 -rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 /tmp/link_new

The shared inode number is 2730074, meaning these files are identical data.

If you want more information on inodes, read my full article here.

Hard limits

While useful, there are some limitations to what hard links can do. For starters, they can only be created for regular files (not directories or special files). Also, a hard link cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.

Soft links

Commonly referred to as symbolic links, soft links link together non-regular and regular files. They can also span multiple filesystems. By definition, a soft link is not a standard file, but a special file that points to an existing file. Let's look at how to create a soft link. I use the ln -s command and the following syntax:

ln -s (file path you want to point to) (new file path)

In the example below, I create a new file at /home/tcarrigan/demo/soft_link_test with the file content soft Hello world. I then create a soft link to that file at /tmp/soft_link_new:

[tcarrigan@server demo]$ ln -s /home/tcarrigan/demo/soft_link_test /tmp/soft_link_new
[tcarrigan@server demo]$ ls -l soft_link_test /tmp/soft_link_new 
-rw-rw-r--. 1 tcarrigan tcarrigan 17 Aug 30 11:59 soft_link_test
lrwxrwxrwx. 1 tcarrigan tcarrigan 35 Aug 30 12:09 /tmp/soft_link_new -> /home/tcarrigan/demo/soft_link_test

Notice that /tmp/soft_link_new is just a symbolic link, pointing to the original /home/tcarrigan/demo/soft_link_test. If I cat the content of /tmp/soft_link_new, I should see the soft Hello world text.

[tcarrigan@server demo]$ cat /tmp/soft_link_new 
soft Hello world

All of this sounds great, but there are some drawbacks to using a soft link. The biggest concern is data loss and data confusion. If the original file is deleted, the soft link is broken. This situation is referred to as a dangling soft link. If you were to create a new file with the same name as the original, your dangling soft link is no longer dangling at all. It points to the new file created, whether this was your intention or not, so be sure to keep this in mind.

Hard or soft?

There is no clear answer here. The best link is the type that fits your particular situation. While these concepts can be tricky to remember, the syntax is pretty straightforward, so that is a plus! To keep the two easily separated in your mind, I leave you with this:

  • A hard link always points a filename to data on a storage device.
  • A soft link always points a filename to another filename, which then points to information on a storage device.

Hopefully, this helps you keep them separated as you work your way through the link types needed to accomplish your daily goals!

[ Free online course: Red Hat Enterprise Linux technical overview. ]

Check out these related articles on Enable Sysadmin

Topics:   Linux   Command line utilities  
Author’s photo

Tyler Carrigan

Tyler is a community manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX