# Understanding Symbolic Links and Hard Links in Linux

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: **hard links** and **soft links** (also known as symbolic links or symlinks). Understanding the differences between these two can help you effectively manage your filesystem.

## What is Link?

Before starting to work with hard and soft links, let's first understand what a link is in Linux.

Every storage device contains files and directories in a collection of blocks.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722007361102/cef2c190-c4fe-4666-a0a5-2c203ff671cb.png align="center")

Information about a file or directory is stored in a data structure called *inode*. Inodes contain metadata such as who is the owner of the file, when it was created and when it was last accessed, its size, permissions, ownership, and pointers to the data blocks where the file's contents are actually stored etc.

Every storage medium, such as hard disks, flash drives, etc., has its own inodes. These inodes are specific to the filesystem, storage device, and partition. So if we move one text file from the local storage device to the flash drive, it will get a different inode.

Every inode has an *inode number* that is unique and used by the filesystem to locate the inode.

A *directory entry* is essentially a table maintained by the filesystem that contains a name for a file or directory and a pointer (reference, often an address) to the inode number.

| Filename | Inode number |
| --- | --- |
| report.txt | 43132770 |
| image.png | 43132771 |

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">We can not directly view the raw directory entry because the filesystem manages it at a low level. However, we can view information about the files in a directory, including inode numbers, which are part of the directory entries using the commands <code>ls -li</code> or <code>stat</code></div>
</div>

A *link* is simply an additional directory entry for a file or directory. It is between the filename and the actual data stored on the filesystem. By creating links we can have multiple directory entries (names) for the same file or directory, allowing two or more names for the same thing.

## Hard Links

A hard link is a directory entry that points directly to the inode of a file. It acts as a separate file and refers to the exact spot on a hard drive. It is a mirror copy of the original file. Let's look at how to create hard links and find out how they work. To create the hard link, use `ln` command.

**Creating a hard link**:

1. Create an original file:
    
    ```bash
    echo "Hello World!" > original.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722515246589/10b8346f-29dd-4255-a86d-2bd244645599.png align="center")
    
2. Create a hard link:
    
    ```bash
    ln original.txt hardlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722515419279/39ddbedb-a62a-4697-89d3-b3d9e64a6cd5.png align="center")
    
3. Verify the inode numbers of the original and hard link files:
    
    ```bash
    ls -i original.txt hardlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722515575361/ae792d3d-6a12-4f1d-9933-4f0b15145c29.png align="center")
    

As we can see, both `original.txt` and `hardlink.txt` share the same inode number, indicating they point to the same file content.

**Editing the hard link**

4. Make changes to the hard link `hardlink.txt`:
    
    ```bash
    echo "Nice to see you World" >> hardlink.txt
    ```
    
5. Check the content of the original file `original.txt`
    
    ```bash
    cat original.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722517146640/509eb247-eba0-42c1-8b47-3e8d389da581.png align="center")
    

As we can see, changes made to `hardlink.txt` are reflected in `original.txt` because they refer to the same inode.

**Deleting the original file**

6. Delete the original file:
    
    ```bash
    rm original.txt
    ```
    
7. Check the content of the hard link `hardlink.txt`:
    
    ```bash
    cat hardlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722518809458/7a2cd0f7-2b91-436a-8a84-0dadc62cabc2.png align="center")
    

As we can see, the file content is still accessible through `hardlink.txt` since the inode and data blocks are preserved until *all* hard links are removed.

**Creating multiple hard links**

8. Create another hard link:
    
    ```bash
    ln hardlink.txt another-hardlink.txt
    ```
    
9. Verify Inode numbers:
    
    ```bash
    ls -li hardlink.txt another-hardlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722519508799/ade1b0e6-5230-406d-abb4-4bf0c3bad95d.png align="center")
    

Again, both `hardlink.txt` and `another-hardlink.txt` share the same inode number, indicating they point to the same file content.

### Hard Link Advantages and Disadvantages

Hard links have the following advantages:

1. **Efficient Storage:** Hard links share the same inode and data blocks, which means no additional disk space is used for multiple hard links to the same file.
    
2. **File Availability**: As long as at least one hard link exists, the file's data remains accessible. Deleting one hard link does not delete the file.
    
3. **Performance**: Hard links are typically faster than symbolic links because they point directly to the inode without the need for path resolution.
    
4. **Transparency**: Hard links appear and behave like regular files. There is no distinction between the original file and its hard links.
    
5. **Consistency**: All hard links to a file are always up-to-date. Any changes made to the file through any hard link are immediately reflected in all other hard links.
    

While useful, there are some limitations to what hard links can do.

1. **Restrictions on Directories**: Hard links can only be created for regular files (not directories or special files).
    
2. **Single Filesystem Limitation**: Hard links cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.
    

## Soft Links

A soft link (symbolic link) is a separate file that contains the path to another file or directory. Unlike hard links, soft links do not reference the inode of the target file directly but instead have their own inodes and contain the path to the target file or directory.

#### Creating a Soft Link

1. Create an original file:
    
    ```bash
    echo "Hello World" > original.txt
    ```
    
2. Create a soft link using the command `ln -s`:
    
    ```bash
    ln -s original.txt softlink.txt
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">An absolute path should be specified when creating symbolic links since relative paths will not work.</div>
    </div>
    
3. Verify the soft link by typing:
    
    ```bash
    ls -l original.txt softlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722602010721/a3460494-6d9d-48c8-a211-a76bfba3ef1c.png align="center")
    
4. Read the content of the `softlink.txt` file:
    
    ```bash
    cat softlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722602238538/f75a30c6-899f-4c72-b280-32c0724a2998.png align="center")
    

#### Editing the Soft Link

5. Edit the soft link `softlink.txt`:
    
    ```bash
    echo "Nice to see you World!" >> softlink.txt
    ```
    
6. Check the content of the `original.txt` file:
    
    ```bash
    cat original.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722602713909/418162cb-0146-44ce-b440-8b8e55c3ad28.png align="center")
    

As we can notice, changes made through `softlink.txt` are reflected in `original.txt`.

**Moving the target file**

7. Move (or delete) the `original.txt` file to another location in the system:
    
    ```bash
    mv original.txt ./backup/
    ```
    
8. Again, check the content of the `softlink.txt` file:
    
    ```bash
    cat softlink.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722603149631/8f59814f-d8e9-47fd-931d-0e27e4e0e2a3.png align="center")
    

As we can see, the soft link becomes a "dangling" link, as its target is moved to another location (or no longer exists in case it is deleted).

#### Linking to Directories

Because the soft link connection is a logical connection, not a duplication, soft links can point to entire directories or links to files on remote computers. Hard links cannot do this. Let's find out how it works.

9. Create a folder and a file:
    
    ```bash
    mkdir my-folder
    echo "Learn how to create soft links to folders" > my-folder/file
    ```
    
10. Create a soft link named `soft-folder` to the directory `my-folder`:
    
    ```bash
    ln -s my-folder soft-folder
    ```
    
11. Verify the link:
    
    ```bash
    ls -l
    ```
    
12. Access the directory `my-folder` using soft link `soft-folder`:
    
    ```bash
    ls soft-folder
    ```
    
13. Delete the original folder `my-folder` and list the `soft-folder`:
    
    ```bash
    rm -r my-folder
    ls soft-folder
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1722684924949/be97646e-daf4-4984-847f-abed2e1d46cf.png align="center")
    

As we can see, since the original folder was removed, listing `soft-folder` shows nothing in the output. The target was deleted and the link became useless.

### Soft Link Advantages and Disadvantages

Soft links have the following advantages:

1. **Flexibility**: Soft links can link to both files and directories and span across different filesystems or partitions.
    
2. **Convenience**: It is easy to create shortcuts and organize files with soft links. They can link to non-existent files, which will work once the target is created.
    
3. **Low Overhead**: The size of soft link files is small since they only store the path to the target.
    

While useful, there are some drawbacks of soft links.

1. **Fragility**: They can become "dangling" links if the target is moved, renamed, or deleted.
    
2. **Performance**: They have slightly slower access than hard links because they require an extra path resolution step.
    
3. **Security**: They can potentially introduce security risks if not managed properly, as they can point to sensitive files.
    
4. **Different Inodes**: Soft links have their own inodes, which can add overhead to inode usage.
    

## References

1. [Linux Crash Course - Symbolic Links](https://www.youtube.com/watch?v=zfSa-PEU3h4&list=PLT98CRl2KxKHKd_tH3ssq0HPrThx2hESW&index=19)
    
2. [Linux Hard Links versus Soft Links Explained](https://www.cbtnuggets.com/blog/certifications/open-source/linux-hard-links-versus-soft-links-explained)
    
3. [Create and change hard and symbolic links](https://developer.ibm.com/tutorials/l-lpic1-104-6/)
    
4. [Hard links and soft links in Linux explained](https://www.redhat.com/sysadmin/linking-linux-explained)
