> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Working With Links

> Explains Linux hard links and symbolic links, inode behavior, when to use each, and a practical ln command demo.

This guide explains how links work on Linux filesystems: what hard links and symbolic (soft) links are, how they interact with inodes, when to use each type, and a short hands‑on demo using the `ln` command.

Links let you reference the same file data from multiple directory entries without duplicating the data on disk. Understanding links helps you manage files, backups, and filesystem layout safely.

## Conceptual analogy

* Symbolic link (symlink): like a signpost that stores a path to a building. If the building is removed or moved, the signpost points to nothing (a dangling link).
* Hard link: like two doors leading into the same room. Both directory entries access the exact same data. Removing one door does not remove the room.

## Inodes — a brief technical note

* An inode is a filesystem structure that stores metadata about a file (permissions, owner, timestamps) and pointers to the file’s data blocks.
* A hard link creates another directory entry that points to the same inode number. Both names reference one inode and therefore the same content.
* A symbolic link is a separate filesystem object (with its own inode) that stores a pathname that refers to the target file or directory.

<Callout icon="lightbulb" color="#1CB2FE">
  Hard links cannot cross filesystem boundaries and, on most systems, you cannot create hard links to directories because that can create filesystem cycles and confuse tools that traverse the directory tree. Symbolic links can point to files or directories, and can cross filesystems.
</Callout>

## When to use which

* Use a hard link when you need another name that references the exact same content on the same filesystem (for example, duplicate names without duplicating data).
* Use a symbolic link when you need to reference files or directories across filesystems or when you need links to directories. Symlinks are also useful for pointing to versioned targets, e.g., `current -> release-2026.10`.

## Quick demo — create links and observe behavior

Follow these steps in a terminal to see hard and symbolic links in action. The commands are shown with a sample prompt to illustrate context; adapt them to your system.

Step-by-step:

```bash theme={null}
# Create working directory and move into it
student@control:~$ mkdir -p links-demo
student@control:~$ cd links-demo

# Create a file with content
student@control:~/links-demo$ printf "Hello World, link me\n" > file1.txt

# Show file listing and link count (initially 1)
student@control:~/links-demo$ ls -l
total 4
-rw-r--r-- 1 student student 21 Apr 15 16:11 file1.txt
```

Create a hard link and inspect inode/link-count:

```bash theme={null}
# Create a hard link to file1.txt
student@control:~/links-demo$ ln file1.txt hard_link_file1

# Now the link count increases to 2 because there are two directory entries to the same inode
student@control:~/links-demo$ ls -l
total 8
-rw-r--r-- 2 student student 21 Apr 15 16:11 file1.txt
-rw-r--r-- 2 student student 21 Apr 15 16:11 hard_link_file1

# Verify both names share the same inode number
student@control:~/links-demo$ ls -i
123456 file1.txt
123456 hard_link_file1

# Both files show identical content
student@control:~/links-demo$ cat hard_link_file1
Hello World, link me

# Modify the original file; the hard link reflects the change because they share the same inode
student@control:~/links-demo$ echo "Appended line" >> file1.txt
student@control:~/links-demo$ cat hard_link_file1
Hello World, link me
Appended line
```

Create a symbolic link and see the difference:

```bash theme={null}
# Create a symbolic (soft) link to file1.txt
student@control:~/links-demo$ ln -s file1.txt soft_link_file1

# Listing shows the symlink pointing to file1.txt (symlink is a separate inode and stores the target path)
student@control:~/links-demo$ ls -l
total 12
-rw-r--r-- 2 student student 35 Apr 15 16:11 file1.txt
-rw-r--r-- 2 student student 35 Apr 15 16:11 hard_link_file1
lrwxrwxrwx 1 student student  9 Apr 15 16:12 soft_link_file1 -> file1.txt
```

Remove the original name to compare behavior of hard vs symbolic links:

```bash theme={null}
# Remove the original file name
student@control:~/links-demo$ rm -f file1.txt

# The hard link remains (it still references the inode that contains the data).
# The link count is decremented to 1.
student@control:~/links-demo$ ls -l
total 8
-rw-r--r-- 1 student student 35 Apr 15 16:11 hard_link_file1
lrwxrwxrwx 1 student student  9 Apr 15 16:12 soft_link_file1 -> file1.txt

# The hard link still provides access to the data
student@control:~/links-demo$ cat hard_link_file1
Hello World, link me
Appended line

# The symbolic link is now dangling because its target path no longer exists
student@control:~/links-demo$ cat soft_link_file1
cat: soft_link_file1: No such file or directory
```

Next, try linking directories:

```bash theme={null}
# Create a directory
student@control:~/links-demo$ mkdir linkme

# Attempt to create a hard link to a directory (usually not permitted)
student@control:~/links-demo$ ln linkme hardlinkme
ln: failed to create hard link 'hardlinkme' => 'linkme': Operation not permitted

# Create a symbolic link to the directory (this works)
student@control:~/links-demo$ ln -s linkme softlink_dir
student@control:~/links-demo$ ls -l
total 8
drwxr-xr-x 2 student student 4096 Apr 15 16:20 linkme
lrwxrwxrwx 1 student student    6 Apr 15 16:20 softlink_dir -> linkme
-rw-r--r-- 1 student student   35 Apr 15 16:11 hard_link_file1
lrwxrwxrwx 1 student student    9 Apr 15 16:12 soft_link_file1 -> file1.txt
```

## Comparison table: hard link vs symbolic link

| Feature | Hard link | Symbolic (soft) link |
| - | - | - |
| Points to | Same inode (data blocks) | Pathname to target file/directory |
| Separate inode | No (shares target inode) | Yes (has its own inode) |
| Cross-filesystem | No | Yes |
| Can link directories | Generally no (not permitted) | Yes |
| Dangling when target removed | No (data remains until all hard links removed) | Yes (becomes broken/dangling) |
| Typical command | `ln <target> <linkname>` | `ln -s <target> <linkname>` |

## Summary

* Hard links add additional directory entries that reference the same inode; they are useful when you want multiple names for the same data on the same filesystem.
* Symbolic links are separate filesystem objects that store a path to the target and can point to files or directories across filesystems.
* Removing a filename that had hard links does not remove the data until all hard links are removed. Removing the target of a symlink makes the symlink dangling.
* Most systems disallow creating hard links to directories to prevent filesystem cycles and traversal issues.

## Links and references

* [GNU coreutils ln documentation](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)
* [Linux man page: ln(1)](https://man7.org/linux/man-pages/man1/ln.1.html)
* [Linux Filesystem Hierarchy & Inodes — overview](https://linuxhandbook.com/inode/)

This concludes the guide on working with links in Linux.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bec5b574-0ba0-45f8-9ddf-03f7cc4f0dd6/lesson/c4687cf4-cbd0-4ef9-b6b3-b97a5d501b31" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.