Linux hard link vs. soft link

Soft links and hard links are the two types of links that can be created on a Linux system. Without realizing it, you have undoubtedly interacted with tons of hard links on your own system. And, most likely, you have encountered some soft links (also called symbolic links or symlinks), too. That is because the files you work with on your system every day are either hard links or soft links that point to data on your hard drive. What we usually think of as a “file” is actually an inode that points to a data block on our hard drive, and the hard link is how we interact with it.

On the other hand, it is useful to think of soft links as “shortcuts” which just point to hard links. Both hard links and soft links have their own use cases, with the former being a way to access inodes stored on our hard drive, and the latter mostly being used for convenient shortcuts. In this tutorial, we will explain hard links vs soft links in Linux. You will learn how they differ from each other, what each is used for, and how to create and manipulate both types of links. Let’s get started!

In this tutorial you will learn:

  • What is a soft link?
  • What is a hard link?
  • How to create hard links and soft links
  • How to delete / unlink hard links and soft links
Linux hard link vs. soft link
Linux hard link vs. soft link
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Hard Links and Soft Links – What’s the Difference?

To fully understand the difference between hard links and soft links, and what purposes they serve, we must first understand how files are stored in Linux. Every file on our system has metadata associated with it, like the creation, modification, and access times, as well as file permissions, etc. This information is stored as an inode. The inode also contains data about where the file’s content exists on the hard drive.

The purpose of an inode is to remember where a file’s data is stored, and all of its associated metadata. The file system uses this information to, for example, know what permissions and file ownership to associate with the file, and where to access the file’s block of data on the hard drive. In order for a user to access an inode, the Linux system provides users with hard links. A hard link points to an inode.

The other type of links on Linux are soft links (or symbolic links). Soft links only point to hard links, and give us an alternative way to access the hard link from other directories or by different names. They are effectively shortcuts. Hard links and soft links both give users the ability to access files and directories via multiple names and paths.

Creating Hard Links

Now let’s see how to create hard links on Linux.

Hard links can’t be used across different file systems, and it is not possible to create a hard link to a directory.
  1. Let’s start by creating a simple file to play around with in our example. The following command creates a file named myfile.txt in the current user’s home directory. It will contain the text “sample text.”
    $ echo "sample text" > ~/myfile.txt
  2. We can check the contents of this file with the cat command. Notice that we access the file via ~/myfile.txt – which is a hard link itself.

    $ cat ~/myfile.txt
    sample text
  3. Now, let’s create a hard link to our new file. This will be the second hard link to our file, since creating it in the steps above is the first hard link. We will create a hard link named myhardlink inside of the system’s /tmp directory. Use the following command syntax with ln:
    $ ln ~/myfile.txt /tmp/myhardlink
  4. Now if we use the cat command on our /tmp/myhardlink hard link, we will find that we get the same contents as if we opened the ~/myfile.txt file.
    $ cat /tmp/myhardlink
    sample text
  5. Next, let’s check the link information associated with both of the files (hard links).
    $ ls -l /tmp/myhardlink
    -rw-rw-r-- 2 linuxconfig linuxconfig 14 May  8 20:20 /tmp/myhardlink
    $ ls -l ~/myfile.txt 
    -rw-rw-r-- 2 linuxconfig linuxconfig 14 May  8 20:20 /home/linuxconfig/myfile.txt

    Notice the associated number 2 as shown by the above output. This number indicates the number of hard links associated with a specific inode.

    At this point, there is no difference between the original ~/myfile.txt file and the hard link we created at /tmp/hardlink. These are both hard links that point to the same inode.

  6. We can remove a hard link with the typical rm command or with the unlink command.
    $ unlink /tmp/myhardlink
  7. Check the file link information once more, and we see that there is now only 1 hard link associated with this inode.
    $ ls -l ~/myfile.txt 
    -rw-rw-r-- 1 linuxconfig linuxconfig 12 Jun  8 20:20 /home/linuxconfig/myfile.txt
  8. Now we can use the rm command to remove our original file:
    $ rm ~/myfile.txt

    At this point the link to the inode pointing to the content of our original file is lost, hence we consider this file as removed. If there are no hard links pointing to an inode, the filesystem may now overwrite the this inode location with a new data.

Creating Soft Links

Contrary to hard links, soft links are able to cross file systems, and it is also possible to create a symbolic link of a directory. However, symbolic links only link to an existing hard links (file or directory name), instead of an inode. For this reason, if the actual hard link to which the symbolic link is pointing to is removed, then the symbolic link becomes broken.

  1. Let’s once again create an example file that we can work with, in order to illustrate how soft links work. Let’s create a new directory called “mydir” and a file inside of it called “myfile” within the current user’s home directory.
    $ mkdir ~/mydir
    $ touch ~/mydir/myfile
  2. Next, we will create a new symbolic link of the existing directory mydir by using the ln command with a combination of the -s option.
    $ ln -s ~/mydir /tmp/symdir
  3. Now, we have created a symbolic link called symdir located within the /tmp directory. Let’s see the link information:
    $ ls -l /tmp/symdir
    lrwxrwxrwx 1 linuxconfig linuxconfig 23 Jun  8 20:34 /tmp/symdir -> /home/linuxconfig/mydir

    Note the first character of the above output. In this case the character l indicates that we are dealing with symbolic link.

    When creating a symbolic links keep in mind that the ln command stores the actual path provided as a string. If not withing the same directory, in many cases you must provide full path to order for the for the symbolic link to work
  4. The newly created symdir symbolic link should contain a the previously created file myfile:
    $ cd /tmp/symdir
    $ ls
    $ pwd

Closing Thoughts

In this tutorial, we saw how hard links and soft links differ from each other and are used on a Linux system. We also learned how to create hard links and soft links, and how to delete them. The takeaway is that while hard links point to an inode, symbolic links only point to hard links. In order to fully delete a file (or at least mark it as overwritable to the file system), all hard links must be erased.

Comments and Discussions
Linux Forum