Symbolic and Hard links in Linux
Published: 2023-01-30 18:28:11

---
title: Symbolic and Hard links in Linux
published: true
date: 2023-01-17 16:40:06 UTC
tags: 
canonical_url: https://zainsiddiqui.hashnode.dev/symbolic-and-hard-links-in-linux
---

I am sure you all have used some GUI-based Operating System once in your life and if that is the case then you might be familiar with the concept of shortcuts. They are typically located on your desktop or within a startup window menu. A shortcut is essentially a file that links to your application so you don't have to go to the installation location every time you want to open it.

In Linux, we can link files to other files, giving us the ability to create our shortcuts. There are two different ways to create links i.e **hard and symbolic**. They achieve the same purpose but both of them work differently and have their pros and cons.

In this article, we'll go over both of them, how they differ from one another and why they are useful to know for sysadmins jobs. So, let's get started.

### Understanding Inodes

Before we go any further, we need to understand the concept of inodes. We won't be going into depth as it will be the topic for some other time. So, Inodes are data objects that store the metadata regarding files and directories such as time stamps, owner of the file, permissions, last modified date, etc. Inodes are represented by an integer number often times called the inode address.

To view the inode address, first, create two dummy files and name them **sample-file-1.txt** and **sample-file-2.txt**.

```
touch sample-file-1.txt sample-file-2.txt

```

Now to view the inode address use the **ls** command with the **-i** flag.

```
ls -i

```

![Checking inode address](https://cdn.hashnode.com/res/hashnode/image/upload/v1673965543689/5c6fb533-35af-4acb-9dfb-c1ea7205cf5b.png)

As you can see, the inode address of both of the files is _4203984_ and _4203990_. The inode address depends upon the filesystem and so it might be different in your case.

### What are Hard Links

A hard link looks like a new file but points back to the data in the original file. They are duplicate entries, where both entries point to the same data. When you are creating a hard link, you're creating another file that points to the exact same data as the original file. It's just a carbon copy of the original file.

Now let's go over how to create a hard link

```
ln sample-file-1.txt link-1.txt

```

Here, I'm creating a hard link _link-1.txt_ that is linked to the _sample-file-1.txt_.

To verify if indeed _link-1.txt_ points to the _sample-file-1.txt_:

```
ls -i

```

![Output after creating hard links](https://cdn.hashnode.com/res/hashnode/image/upload/v1673965651695/e988df78-12fc-4bdc-8507-4d2bf539f61d.png)

In the above output, the important thing to note is that _sample-file-1.txt_ and _link-1.txt_ have the same inode address which is proof that the hard link that we created points to the original file. With this hard link created, we can move _link-1.txt_ to another location and it will still work.

Hard links are very easy to set up and use but this simplicity has its own shortcomings. First, you can only create a hard link for files and not for directories. Secondly, hard links can not be moved to a different filesystem as hard links depend upon the inode address and each filesystem has its own inode address.

### Soft Links or Symbolic Links

To overcome this limitation, we have another type of link i.e symbolic links or soft links. As you might recall, hard links are duplicate entry that points to the same inode address. But, a symbolic link is an entry that points to another directory or file. This is different from the hard link as it only references a specific path.

To create a soft link use the following command:

```
ln -s sample-file-2.txt link-2.txt
ls -i

```

![Output after creating soft links](https://cdn.hashnode.com/res/hashnode/image/upload/v1673965710576/53e3860d-8ce4-4dfa-911b-508b35d9b4ed.png)

Notice that the inode addresses are different. Symbolic links are not duplicate entries of the original file but the new file that points to the original file path. It works exactly in the same way as pointers work in C.

There are several advantages of using soft links such as it works for both files and directories. You can move soft links to a different file system and it will work as expected as it does not have any dependency on inodes.

Symbolic links have some limitations as well. As the symbolic link is just a pointer to the original file, if you move the original file or directory to another location, your link will break as the symbolic link won't be able to find the original file path. Another thing to note is that symbolic links don't inherit the original file permissions and this means changes to the original file permissions won't be reflected in a symbolic link.

### Wrap up

As we have covered both hard and symbolic links, you might be wondering which one to use. The main benefit of the hard link is that you can move either the original file or the link to any different location and it will still work. This is not true for the symbolic links as it is just a pointer to the original file location. If you delete the original file, the hard link will still work but the symbolic link will break. The reason is straightforward, hard links are duplicates of the original file while symbolic links are just merely a pointer to the actual file path.

Taking into account the benefits of hard links versus soft links, you should prefer hard links only when you are sure that you might need to move links to different locations. Otherwise, you should prefer using symbolic links as they work with both files and directories and are filesystem independent.

This is it for this article, I hope you gained something valuable out of it. If you like this article and would like me to continue writing these types of articles then please give it a thumbs up. If you have any suggestions or questions feel free to use the comment section below.