> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/computer-science/linux/hard-link-vs.-symlink.md).

# Hard Link vs. Symlink

## Hard Link vs. Symlink

Hard links point to inode and symlinks point to the file. Visually:

![Visualized path difference between hard link and symbolic link references](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWVjG_njKgBtvmnKaJh%2F-Mftk9kxtJuFxejazUGr%2F-MftkMDQYDX0yC4RTiov%2Fimage.png?alt=media\&token=25725515-b82f-4538-ad4e-54cb6b9b9987)

## Inode

The **inode** is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive.

## Hard Link

A **hard link** is a direct reference to a file via its **inode**. By using a hardlink, you can change the original file’s contents or location and the hardlink will still point to the original file because its inode is still pointing to that file. **There is no referencing to the original file.**

For example, we create a hard link of the file `fun` and name it `fun-hard`:

```bash
ln fun fun-hard
```

The original file `fun` and the hard link `fun-hard` share the **same inode number**, so we can confirm that they are the same file:

![Hard link](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWVjG_njKgBtvmnKaJh%2F-MftedeLL00DtRRHN_7X%2F-Mftjq7ovQgQtxxMBB1t%2Fimage.png?alt=media\&token=9f94f954-3df8-4fc9-98de-a05b30a7dd08)

Hardlinks have two important **limitations**:

1. A hard link cannot reference a file outside its own file system. This means a link cannot reference a file that is not on the same disk partition as the link itself.
2. A hard link may not reference a directory.

## Symlink

**Symbolic links (symlinks)** were created to overcome the limitations of hard links. Symlinks are essentially shortcuts that reference to a file instead of its inode value. If the original file is deleted, then its symlink becomes broken link or dangling link.

For example, we create a symlink of the file `fun` and name it `fun-sym`:

```bash
ln -s fun fun-sym
```

The symlink is represented by the l bit, its size is small, and Linux indicates the symlink using an arrow:

![Symlink](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWVjG_njKgBtvmnKaJh%2F-Mftm1M_HnywL98rlZY9%2F-Mftq4pXbkx383cLAJKE%2Fimage.png?alt=media\&token=93b7243a-c5e0-48d2-849d-08fb86f0c414)

If we delete fun, then fun-sym becomes a broken link. Linux marks a broken link as red:

![Broken link](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWVjG_njKgBtvmnKaJh%2F-Mftm1M_HnywL98rlZY9%2F-MftqJixLaYlfyEn2-wX%2Fimage.png?alt=media\&token=90ee1228-9b6b-49db-b67c-561719b2a78a)

## Reference

{% embed url="<https://medium.com/@307/hard-links-and-symbolic-links-a-comparison-7f2b56864cdd>" %}
Hard links and Symbolic links — A comparison
{% endembed %}
