> 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](/files/-MftkMDQYDX0yC4RTiov)

## 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](/files/-Mftjq7ovQgQtxxMBB1t)

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](/files/-Mftq4pXbkx383cLAJKE)

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

![Broken link](/files/-MftqJixLaYlfyEn2-wX)

## Reference

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