DEV Community

Shweta Thikekar
Shweta Thikekar

Posted on

Mastering Linux File Systems: Everything You Need to Know About Symlinks and Hard Links

What is file system?

A file system in Linux is the method used to organize and store data on storage devices such as hard drives, SSDs, or USB drives. It defines how files are named, stored, accessed, and managed on the disk. The Linux file system is a hierarchical structure where everything is treated as a file, whether it’s data, directories, devices, or even processes.

Hierarchical Structure: The file system is organized like a tree, starting from the root directory /.

Key Concepts of the Linux File System

1. Everything is a File:

In Linux, everything is treated as a file, whether a text document, a directory, a device, or a process.

Example:

  • Regular files: Text, binaries, images, etc.
  • Directories: Special files that store references to other files.
  • Devices: Represented as files in /dev (e.g., /dev/sda for a hard drive).

2. Hierarchical Directory Structure:

  • The Linux file system starts with a single root directory /.
  • All other directories and files branch out from this root in a tree-like structure.

Example: /home/user/Documents/file.txt

Important Linux Directories

Linux Directories

3. File Types:

  • Regular files: Contain data (e.g., /etc/hosts).
  • Directories: Store files and other directories.
  • Symbolic links: Pointers to other files or directories.
  • Device files: Interface to hardware (e.g., /dev/null).
  • Sockets and pipes: Used for inter-process communication.

4. File System Types:

Image description

Special Symbols:

.: Current directory.
..: Parent directory.
~: Home directory of the current user.


Symlinks

A symlink (symbolic link) is a special type of file that points to another file or directory. A symlink is like a shortcut pointing to a file or folder. The difference between absolute and relative symlinks is how they point to the target. There are two types of symlinks:

Types of Symlinks

1. Absolute Symlink

An absolute symlink uses the full path to point to the target file or folder.

Example:

  • If your target file is at /home/shweta/test/file.txt, the symlink will remember this full path.

Think of it as:

  • Giving someone your complete home address (e.g., "123 Main Street, Mumbai") to reach you.

How to Create It:
ln -s /home/shweta/test/file.txt /home/shweta/shortcut.txt

2. Relative Symlink

A relative symlink uses a shorter path, relative to the symlink's location.

Example:

  • If you're in /home/shweta/ and want to link to test/file.txt, the symlink will only remember "test/file.txt", not the full path.

Think of it as:

  • Telling someone how to reach you from where they currently are (e.g., "Just walk 2 blocks to Main Street").

How to Create It:
ln -s test/file.txt relative_shortcut.txt

Key Difference

  • Absolute symlink: Always works because it has the full address.
  • Relative symlink: Works only if you don't move the symlink or its target around.

Practice Example

Create Absolute Symlink:
ln -s /etc/passwd absolute_link.txt

This creates a shortcut absolute_link.txt to /etc/passwd.

Create Relative Symlink:
ln -s ../test/file.txt relative_link.txt

This creates a shortcut relative_link.txt to a file relative to the symlink's location.

A symlink breaks when it points to a file or directory that no longer exists or cannot be found. This happens because the symlink is just a pointer; it doesn’t store the content itself. Here's how and why symlinks break:

Why Does a Symlink Break?

Target is Deleted:

  • If the file or directory the symlink points to is removed, the symlink has nothing to reference.

Example:
ln -s /home/shweta/test/file.txt shortcut.txt
rm /home/shweta/test/file.txt # Deletes the target file
Now, shortcut.txt is broken.

Target is Moved or Renamed:
-If the target file or folder is moved or renamed, the symlink’s pointer becomes invalid.

Example:
ln -s /home/shweta/test/file.txt shortcut.txt
mv /home/shweta/test/file.txt /home/shweta/test/renamed_file.txt
shortcut.txt still points to /home/shweta/test/file.txt, which no longer exists.

Relative Symlink Context Changes:

  • If a relative symlink is moved to a new location, it might not align correctly with its target anymore.

Example:
ln -s ../file.txt relative_link.txt
mv relative_link.txt /some/other/path/
The relative_link.txt will now break because its target is calculated based on its original location.

How to Fix a Broken Symlink

Recreate the Target File/Folder:

  • If the target was deleted, recreate it at the original path.

Update the Symlink to a New Target:

  • Use the ln -sf command to update the symlink to a valid target.

Example:
ln -sf /new/path/to/file.txt shortcut.txt

Delete the Broken Symlink:
If it’s no longer needed, remove it.

Example:
rm shortcut.txt

Pro Tip to Avoid Broken Symlinks

  • Use absolute symlinks when the target is unlikely to move or when reliability is critical.
  • Use relative symlinks only for files/folders that are part of the same structure and will move together.

Absolute Path Symlink

How it works: Points to the full, unchanging path of the target file.

Behavior when moved:

  • Shortcut (symlink) moved: It does not break because the symlink still references the target's full path.
  • Target moved: It breaks because the absolute path to the target is no longer valid.

Relative Path Symlink

How it works: Points to the target relative to the symlink’s location.

Behavior when moved:

  • Shortcut (symlink) moved: It breaks because the relative path calculation becomes invalid in the new location.
  • Target moved: It breaks because the symlink’s relative reference doesn’t update with the target's new location.

Hard Links

A hard link is a secondary name for a file that refers directly to the same inode (a file system data structure). Unlike symlinks, hard links are not pointers but direct references to the actual data of a file.

Let’s break it down with simple explanations, examples, and practical tips.

Key Characteristics of Hard Links

  • Shared Inode:

Hard links share the same inode as the original file.
Inodes store metadata (e.g., file permissions, size, timestamps) and point to the actual data blocks.

  • Independent of the Original File's Path:

Even if the original file is deleted, the data remains accessible through its hard link(s).
The link count (seen in ls -l) must drop to zero for the file to be truly deleted.

  • Cannot Span File Systems:

Hard links must exist within the same filesystem because they reference inodes directly.

  • Directories Are Excluded:

Hard links cannot be created for directories due to filesystem consistency and potential looping issues.

How to Create a Hard Link

Command:
ln <target_file> <hard_link>

Example:

touch original.txt           # Create a file
ln original.txt link.txt     # Create a hard link
Enter fullscreen mode Exit fullscreen mode

This creates a hard link link.txt that points to the same inode as original.txt.

Checking Hard Links

Use the ls -li command to view inode numbers:
ls -li

Example output:

256700 -rw-r--r--  2 user group 1024 Dec 28 10:00 original.txt  
256700 -rw-r--r--  2 user group 1024 Dec 28 10:00 link.txt
Enter fullscreen mode Exit fullscreen mode

Both files share the same inode (256700), confirming they are hard links.
The second column (2) shows the number of links to the file.

Behavior of Hard Links

  • File Deletion:

If original.txt is deleted, the file content remains accessible via link.txt because the data blocks are still referenced.

  • Modifications:

Changes made to either the original file or the hard link reflect in the other since they point to the same data blocks.

  • Storage:

Hard links consume no extra disk space for the file’s data, only a directory entry.

Differences Between Hard Links and Symlinks

Image description

Use Cases of Hard Links

  • Preserve Data:

Use hard links to ensure file data is accessible even if the original file is accidentally deleted.

  • Efficient Backups:

Hard links avoid duplicating file data, saving space in backup systems.

  • Organizing Files:

Use multiple names for the same file in different directories without using additional storage.

Breaking Scenarios

Unlike symlinks, hard links are robust against most changes. However, there are a few limitations:

  • File System Boundaries:

You cannot create hard links across filesystems (e.g., linking a file on /home to /tmp).

  • Root-Level Management:

Hard links are harder to identify and manage because they share inodes invisibly to users.

Practice Example

  • Create a Hard Link:

ln /home/shweta/notes.txt /home/shweta/backup_notes.txt

This creates a hard link named backup_notes.txt pointing to notes.txt.

  • Verify:

ls -li /home/shweta/notes.txt /home/shweta/backup_notes.txt

  • Output:
128734 -rw-r--r-- 2 shweta group 2048 Dec 28 10:00 notes.txt  
128734 -rw-r--r-- 2 shweta group 2048 Dec 28 10:00 backup_notes.txt
Enter fullscreen mode Exit fullscreen mode

Conclusion:
Understanding the Linux file system is fundamental for efficient data management and system administration. The hierarchical structure and features like symlinks and hard links offer flexibility and robustness in handling files and directories. By grasping the differences between absolute and relative symlinks and the advantages of hard links, you can enhance your file organization, create reliable shortcuts, and safeguard critical data. Whether you're a beginner exploring Linux or a seasoned professional, mastering these concepts equips you to navigate the Linux ecosystem confidently and efficiently.

Top comments (0)