Table of Contents
Linux allows multiple names to point to the same file — and it does this through something called links.
There are two types of links in Linux: hard links and soft links (also called symbolic links).
Understanding both helps you manage files efficiently, save space, and avoid accidental data loss.
What Are Links in Linux?
Every file in Linux has two parts:
- The inode – stores metadata and points to the file’s data blocks on disk
- The filename – a label (directory entry) that points to that inode
A “link” is simply another name or reference that connects to the same file — either directly to the inode or to the file path.
Hard vs Soft Links Diagram — how filenames map to inodes or paths.
Hard Links
A hard link is another real name for the same file (the same inode).
When you create a hard link, Linux doesn’t copy the data — it simply adds a new directory entry that points to the same inode.
Let’s see it in action.
Step 1: Create a File
echo "hello world" > fileA
List it with inode information:
ls -li fileA
Output:
2684 -rw-r--r-- 1 root root 12 Oct 21 fileA
- The inode number is
2684. - The link count is
1— only one name points to this inode.
Step 2: Create a Hard Link
ln fileA fileB
Syntax explanation:
ln <existing_file> <new_link_name>
ln→ the link commandfileA→ the existing (original) filefileB→ the new name that will point to the same inode
Now list both:
ls -li fileA fileB
Output:
2684 -rw-r--r-- 2 root root 12 Oct 21 fileA
2684 -rw-r--r-- 2 root root 12 Oct 21 fileB
- Both share the same inode number (2684).
- The link count is now
2. fileAandfileBare equal names for the same underlying file.
Step 3: Edit One — See the Change in Both
echo "another line" >> fileB
cat fileA
Output:
hello world
another line
Both names refer to the same data — change one, and the other updates automatically.
Step 4: Delete One — The File Still Exists
rm fileA
ls -li fileB
Output:
2684 -rw-r--r-- 1 root root 24 Oct 21 fileB
The file still exists! The inode and data remain until all hard links are removed.
Soft Links (Symbolic Links)
A soft link (or symbolic link) is a shortcut that points to another file’s path, not its inode. This makes it more flexible — but also more fragile.
Let’s recreate fileA and make a symbolic link (fileB) that points to it.
Step 1: Recreate the Original File
echo "testing symbolic link" > fileA
Step 2: Create a Soft Link
ln -s fileA fileB
Syntax explanation:
ln -s <target_file> <link_name>
ln→ the link command-s→ tells Linux to create a symbolic (soft) link instead of a hard onefileA→ the target file that the link will point tofileB→ the shortcut name that will reference the path tofileA
Now list both:
ls -l
Output:
-rw-r--r-- 1 root root 23 Oct 21 fileA
lrwxrwxrwx 1 root root 5 Oct 21 fileB -> fileA
fileAis the real file.fileBis a symbolic link pointing to the pathfileA.- The arrow (
->) shows this path relationship.
Step 3: Read Through the Soft Link
cat fileB
Output:
testing symbolic link
Linux follows the link path and reads the content of fileA.
Step 4: Delete the Target (fileA)
rm fileA
cat fileB
Output:
cat: fileB: No such file or directory
The soft link (fileB) still exists, but it’s now broken — it points to a file path that no longer exists. Unlike hard links, symbolic links depend on the path being valid.
Key Differences
| Feature | Hard Link | Soft (Symbolic) Link |
|---|---|---|
| Points to | The file’s inode | The file’s path |
| Works across drives or partitions | No | Yes |
| Works with directories | No | Yes |
| Target deleted? | Link still works | Link breaks (dangling link) |
| Inode number | Same as original | Different inode |
| Command | ln fileA fileB |
ln -s fileA fileB |
| Windows equivalent | NTFS hard link | Shortcut (.lnk) |
Real-World Use Cases
Hard Links
- Save disk space (no duplicate files)
- Keep files safe from accidental deletion
- Used by package managers and backup systems
Soft Links
- Used for configuration files and shortcuts
- Can link across drives or directories
- Common for
/etc/alternativesand/usr/binshortcuts
Commands Summary
| Action | Command |
|---|---|
| Create a hard link | ln fileA fileB |
| Create a soft link | ln -s fileA fileB |
| View inode numbers | ls -li |
| Identify broken soft links | find . -xtype l |
TL;DR
Hard links are multiple real names pointing to the same file (same inode). Soft links are shortcuts that point to a file’s path.
Hard links give you durability and efficiency inside one filesystem. Soft links give you flexibility across different locations.
Final Thoughts
Both types of links solve different problems. Once you understand inodes and paths, you’ll start to see why Linux gives you both options — hard links for safety and efficiency, and soft links for flexibility and convenience.
Start the conversation