0
0
Linux CLIscripting~15 mins

ln (hard and symbolic links) in Linux CLI - Deep Dive

Choose your learning style9 modes available
Overview - ln (hard and symbolic links)
What is it?
The 'ln' command in Linux creates links between files. There are two types: hard links and symbolic (soft) links. Hard links point directly to the file's data on disk, while symbolic links point to the file's name or path. These links let you access the same file from different places without copying it.
Why it matters
Without links, you would need to duplicate files to access them from multiple locations, wasting disk space and making updates harder. Links save space and keep files consistent because changes to one link affect the original file. This is crucial for organizing files, managing backups, and creating shortcuts.
Where it fits
Before learning 'ln', you should understand basic Linux file system concepts like files, directories, and paths. After mastering links, you can explore file permissions, file system structure, and advanced shell scripting that uses links for automation.
Mental Model
Core Idea
Links are pointers that let multiple names or paths refer to the same file data without duplicating it.
Think of it like...
Imagine a library where a book can have multiple index cards pointing to the same shelf spot (hard link), or a sticky note on the catalog pointing to where the book is located (symbolic link). Both help you find the book without making copies.
File System Structure:

Original File: [file.txt] (data block #123)

Hard Link 1: [file.txt] -> points directly to data block #123
Hard Link 2: [copy.txt] -> points directly to data block #123

Symbolic Link: [shortcut.txt] -> points to 'file.txt' path

Diagram:

[file.txt]───┐
             │
             ├──> data block #123
             │
[copy.txt]───┘

[shortcut.txt] ---(points to path)---> [file.txt]
Build-Up - 7 Steps
1
FoundationUnderstanding Files and Inodes
🤔
Concept: Files in Linux are represented by inodes, which store data and metadata separately from filenames.
Every file has an inode number that holds its data location and attributes. Filenames are just labels pointing to these inodes. Multiple filenames can point to the same inode, which is the basis for hard links.
Result
Knowing that filenames are separate from file data helps understand how links can share the same data without copying.
Understanding that files are more than just names is key to grasping how links work under the hood.
2
FoundationBasic Usage of ln Command
🤔
Concept: The 'ln' command creates links between files, with options for hard or symbolic links.
To create a hard link: ln original.txt link.txt To create a symbolic link: ln -s original.txt shortcut.txt Hard links share the same inode; symbolic links are separate files pointing to a path.
Result
You get new file entries that either share data (hard) or point to the original file path (symbolic).
Knowing the basic commands lets you create links and see their differences in practice.
3
IntermediateDifferences Between Hard and Symbolic Links
🤔Before reading on: do you think deleting the original file breaks both hard and symbolic links? Commit to your answer.
Concept: Hard links are indistinguishable from the original file and remain valid if the original is deleted; symbolic links break if the original file is removed or moved.
Hard links share the same inode number as the original file. Deleting one link does not delete the data until all links are removed. Symbolic links store a path and become broken if the target is missing.
Result
Hard links provide robust references; symbolic links are flexible but fragile if targets change.
Understanding link behavior after deletion helps avoid broken references and data loss.
4
IntermediateLimitations of Hard Links
🤔Before reading on: can hard links point to directories or files on other drives? Commit to your answer.
Concept: Hard links cannot link directories or files across different file systems; symbolic links can.
Hard links must be on the same file system and cannot link directories to avoid loops. Symbolic links can point anywhere, including directories and other file systems.
Result
You must choose symbolic links for cross-filesystem or directory linking.
Knowing these limits prevents errors and helps choose the right link type.
5
IntermediateUsing ls and stat to Inspect Links
🤔
Concept: Commands like 'ls -l' and 'stat' reveal link types and inode numbers to understand links better.
ls -l shows symbolic links with 'l' and their target path. ls -li shows inode numbers to identify hard links. stat shows detailed inode and link count info.
Result
You can distinguish link types and see how many hard links point to the same inode.
Inspecting links with commands deepens understanding and aids troubleshooting.
6
AdvancedLink Count and File Deletion Behavior
🤔Before reading on: does deleting a hard link immediately remove the file data? Commit to your answer.
Concept: The file data remains on disk until all hard links are deleted; the link count tracks this.
Each hard link increments the inode's link count. Deleting a hard link decrements it. Only when the count reaches zero does the system free the data blocks. Symbolic links do not affect link count.
Result
Files can persist even if the original name is deleted, as long as hard links exist.
Understanding link counts prevents accidental data loss and explains file persistence.
7
ExpertSymbolic Link Pitfalls and Race Conditions
🤔Before reading on: do you think symbolic links are always safe to use in scripts? Commit to your answer.
Concept: Symbolic links can cause race conditions and security issues if the target changes unexpectedly during script execution.
Scripts following symbolic links may operate on unexpected files if the link target changes between checks and actions. This can lead to security vulnerabilities or data corruption. Careful validation and atomic operations are needed.
Result
Scripts must handle symbolic links cautiously to avoid unpredictable behavior.
Knowing symbolic link risks helps write safer, more reliable automation scripts.
Under the Hood
Linux file systems store file data in inodes identified by numbers. Hard links are directory entries that point directly to the same inode number, sharing the same data blocks. Symbolic links are special files containing a path string to another file. The kernel resolves symbolic links by reading this path and accessing the target file. The link count in the inode tracks how many hard links exist. When the count drops to zero, the system frees the data blocks.
Why designed this way?
This design separates file identity (inode) from names (directory entries) to allow multiple names for the same data without duplication. Hard links provide efficient storage and data consistency. Symbolic links add flexibility by allowing links across file systems and to directories, at the cost of potential fragility. This balance supports diverse use cases in Unix-like systems.
┌───────────────┐        ┌───────────────┐
│ Directory 1   │        │ Directory 2   │
│ file.txt (ln) │───────▶│ hardlink.txt  │
└──────┬────────┘        └───────────────┘
       │
       ▼
   ┌─────────┐
   │ Inode #1│
   │ Data    │
   └─────────┘

┌───────────────┐
│ symlink.txt   │
│ (symbolic)    │
│ points to     │
│ file.txt path │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does deleting the original file break hard links? Commit to yes or no.
Common Belief:Deleting the original file deletes all hard links too.
Tap to reveal reality
Reality:Hard links are equal references to the same data; deleting one name does not delete the data if other hard links exist.
Why it matters:Believing this causes unnecessary fear of data loss and misunderstanding of file persistence.
Quick: Can hard links point to directories? Commit to yes or no.
Common Belief:Hard links can be made to directories just like files.
Tap to reveal reality
Reality:Most systems forbid hard links to directories to prevent file system loops and complexity.
Why it matters:Trying to create hard links to directories leads to errors and confusion.
Quick: Are symbolic links always safe to use in scripts? Commit to yes or no.
Common Belief:Symbolic links are just like shortcuts and always safe to use.
Tap to reveal reality
Reality:Symbolic links can cause race conditions and security risks if the target changes unexpectedly.
Why it matters:Ignoring this can lead to script failures or security vulnerabilities.
Quick: Do symbolic links use the same inode as the target file? Commit to yes or no.
Common Belief:Symbolic links share the inode number with their target file.
Tap to reveal reality
Reality:Symbolic links have their own inode because they are separate files containing a path string.
Why it matters:Misunderstanding this leads to confusion when inspecting files and link counts.
Expert Zone
1
Hard links cannot cross file system boundaries because inodes are unique only within a single file system.
2
Symbolic links can be relative or absolute paths, affecting their behavior when directories move.
3
The link count in an inode includes only hard links, not symbolic links, which affects file deletion logic.
When NOT to use
Avoid hard links when linking directories or files across different file systems; use symbolic links instead. Avoid symbolic links in security-sensitive scripts without proper validation due to potential race conditions.
Production Patterns
Hard links are used in backup systems to save space by linking unchanged files. Symbolic links are common for configuration files, version management, and creating user-friendly shortcuts.
Connections
Pointers in Programming
Both links and pointers refer to data locations rather than duplicating data.
Understanding links as filesystem pointers helps grasp memory management concepts in programming.
URL Shorteners
Symbolic links act like URL shorteners by redirecting to a target path.
Knowing how symbolic links redirect helps understand web redirection and aliasing.
Library Catalog Systems
Hard links are like multiple catalog cards pointing to the same book location.
This connection clarifies how multiple references can point to one resource without duplication.
Common Pitfalls
#1Creating a hard link to a directory.
Wrong approach:ln /home/user/docs /home/user/docs_link
Correct approach:ln -s /home/user/docs /home/user/docs_link
Root cause:Misunderstanding that hard links to directories are generally disallowed to prevent file system loops.
#2Using symbolic links without checking if the target exists.
Wrong approach:ln -s /nonexistent/file.txt link.txt cat link.txt
Correct approach:ln -s /existent/file.txt link.txt cat link.txt
Root cause:Not verifying the target path leads to broken links and errors when accessing.
#3Assuming deleting the original file deletes all links.
Wrong approach:rm original.txt # Expecting all hard links to be gone
Correct approach:rm original.txt # Hard links still access the data until all are removed
Root cause:Confusing file names with file data and not understanding link counts.
Key Takeaways
The 'ln' command creates hard and symbolic links that let multiple names refer to the same file data or path.
Hard links share the same inode and data, surviving deletion of any single name until all links are removed.
Symbolic links are separate files pointing to a path and can link across file systems and directories but can break if targets move or delete.
Understanding link counts and inode structure is essential to managing file persistence and avoiding data loss.
Careful use of symbolic links is needed in scripts to avoid security risks and race conditions.