What are hard and symbolic links on Linux, how to create them, and what is the difference between the two

What are hard and symbolic links on Linux, how to create them, and what is the difference between the two


No hay texto alternativo para esta imagen

symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file. If you delete the original file, the soft link has no value, because it points to a non-existent file.

But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.

In a nutshell, a soft link

  • can cross the file system,
  • allows you to link between directories,
  • has different inode number and file permissions than original file,
  • permissions will not be updated,
  • has only the path of the original file, not the contents.

A hard Link

  • can't cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
  • can't link directories,
  • has the same inode number and permissions of original file,
  • permissions will be updated if we change the permissions of source file,
  • has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

Still don't get it? Well, allow me to show you some practical examples.

The inodes

No hay texto alternativo para esta imagen

An inode is something that contains the full description of a file or directory, including what it contains and its location on the hard drive. An inode does not have a fixed value. Indeed, it contains several data which have variable values which will change according to the use. It can be considered as the ID Card of a file or directory.

Creating Hard Link

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.

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.

Creating Soft Link or Symbolic Link

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.

What is the difference between a hard link and a symbolic link?

Both hard links and symbolic links are methods of linking one file to another on a hard drive. A system is made up of several files which are each referenced on the hard drive by what is called an inode. In short before going into details, we will see that a hard link is a copy of a file that points to the same inode, when a symbolic link is linked only to the file in question.

As said above, a hard link is a direct link to the inode of a file. that is, no matter what changes you make to one or the other, both will always point to the same inode. Without going into technical details, here is a file named "file .txt" and a corresponding hard link named "hardlink". Both files are initially empty. By modifying either one or the other, the value of both changes. As you can see, with the ls -i command, we can see which inode each file is attached to. Note that the two files I created have the same inode. Which explains why their value is exactly the same.

Is a hard link definitive?

Well, yeah. A hard link, as long as it is not deleted is definitive. Indeed, because it is not directly attached to the initial file, it does not matter if this one still exists, the hard link will always have all the necessary information to be functional.

This is where we find the difference with a symbolic link.

You know that a symbolic link is linked only to the file and not to its inode. Did you understand the difference? In fact, a symbolic link is like a URL that we use to go to a website for example. If one day the server on which the site is hosted has to be deleted, the URL would point to nothing. The same goes for a symbolic link.

If the file the symbolic link points to is deleted or moved, this will cause the symbolic link to become broken. Otherwise, a symbolic link works like a hard link. Anything that is changed on one will change on the other.

Another small difference is that since the symbolic link is linked to the file, if the file is removed, the link will be broken. BUT if a new file is created and named identically, the link will be functional again and will now point to the new file.

So, this link is not final if the original file is deleted. It is simply more flexible than a hard link because it is not linked to the inode but to the file itself.


To view or add a comment, sign in

More articles by Cesar Molina

  • What does “STEM” mean?

    You may have heard of the importance of “STEM education” and “STEM jobs.” It seems like everyone is talking about it…

  • What happens when you type google.com in your browser and press Enter

    The World wide web, or simply the Web, has been around for quite a while now and it has altered our lives in almost…

  • Internet of things

    There’s a lot of noise at the moment about the Internet of Things (or IoT) and its impact on everything from the way we…

  • Python3: Mutable, Immutable... everything is object!

    Mutable, Immutable… everything is an object! Python is an object-oriented programming language, and in Python…

  • Differences between static and dynamic libraries

    The main difference between static and dynamic linking is that static linking copies all library modules used in the…

  • C static libraries

    In the C programming language, a static library is a compiled object file containing all symbols required by the main…

  • Compiling a C program with gcc

    By Cesar Molina Compiling Compiling is the process of transforming a high level language into a low level langauge. A…

Insights from the community

Others also viewed

Explore topics