Bird
Raised Fist0
Linux CLIscripting~15 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between a hard link and a symbolic link in Linux?
easy
A. A hard link can only link to directories; a symbolic link can only link to files.
B. A hard link is a shortcut to a directory; a symbolic link is a shortcut to a file.
C. A hard link points directly to the file's data, sharing the inode; a symbolic link points to the file name.
D. A hard link uses ln -s; a symbolic link uses ln.

Solution

  1. Step 1: Understand hard link behavior

    Hard links share the same inode number and point directly to the file data, making them indistinguishable from the original file.
  2. Step 2: Understand symbolic link behavior

    Symbolic links are special files that contain a path to another file or directory, acting like shortcuts pointing to file names.
  3. Final Answer:

    A hard link points directly to the file's data, sharing the inode; a symbolic link points to the file name. -> Option C
  4. Quick Check:

    Hard link = inode sharing, Symbolic link = filename pointer [OK]
Hint: Hard links share inode; symbolic links point to names [OK]
Common Mistakes:
  • Confusing which link shares inode
  • Thinking symbolic links share file data
  • Mixing up commands for creating links
2. Which command correctly creates a symbolic link named linkfile pointing to originalfile?
easy
A. ln -s originalfile linkfile
B. ln linkfile originalfile
C. ln -s linkfile originalfile
D. ln originalfile linkfile

Solution

  1. Step 1: Recall symbolic link syntax

    The syntax for creating a symbolic link is ln -s <target> <link_name>.
  2. Step 2: Match command to syntax

    ln -s originalfile linkfile uses ln -s originalfile linkfile, which correctly creates a symbolic link named linkfile pointing to originalfile.
  3. Final Answer:

    ln -s originalfile linkfile -> Option A
  4. Quick Check:

    ln -s target link_name [OK]
Hint: Use ln -s target link_name for symbolic links [OK]
Common Mistakes:
  • Swapping target and link name order
  • Omitting -s for symbolic links
  • Using ln -s with wrong argument order
3. Given the commands:
echo "Hello" > file1
ln file1 file2
ln -s file1 file3
rm file1

What will be the output of cat file2 and cat file3?
medium
A. file2: Hello
file3: Hello
B. file2: cat: file2: No such file or directory
file3: cat: file3: No such file or directory
C. file2: cat: file2: No such file or directory
file3: Hello
D. file2: Hello
file3: cat: file3: No such file or directory

Solution

  1. Step 1: Understand hard link behavior after deleting original

    Hard link file2 points to the same inode as file1. Removing file1 does not delete the data because file2 still references it. So cat file2 outputs "Hello".
  2. Step 2: Understand symbolic link behavior after deleting original

    Symbolic link file3 points to the name file1. After deleting file1, file3 points to a non-existent file, so cat file3 gives an error.
  3. Final Answer:

    file2: Hello
    file3: cat: file3: No such file or directory
    -> Option D
  4. Quick Check:

    Hard link survives deletion; symbolic link breaks [OK]
Hint: Hard links keep data after original deleted; symbolic links break [OK]
Common Mistakes:
  • Assuming symbolic link still works after target deletion
  • Thinking hard link deletes data with original
  • Confusing link types' behavior on file removal
4. You run the command ln -s /path/to/file linkname but get an error: ln: failed to create symbolic link 'linkname': File exists. What is the best way to fix this?
medium
A. Delete the target file and rerun the command.
B. Use ln -sf /path/to/file linkname to force overwrite the existing link.
C. Use ln /path/to/file linkname without -s.
D. Rename the target file before creating the link.

Solution

  1. Step 1: Understand the error cause

    The error occurs because linkname already exists, and ln refuses to overwrite existing files or links by default.
  2. Step 2: Use force option to overwrite

    Using ln -sf forces the symbolic link creation by overwriting the existing linkname safely.
  3. Final Answer:

    Use ln -sf /path/to/file linkname to force overwrite the existing link. -> Option B
  4. Quick Check:

    ln -sf overwrites existing links safely [OK]
Hint: Use -sf to force overwrite existing symbolic links [OK]
Common Mistakes:
  • Deleting the target file instead of the link
  • Omitting -s and creating hard link by mistake
  • Renaming target unnecessarily
5. You want to create a backup system where multiple filenames point to the same file data without duplicating storage. Which command and link type should you use, and why?
hard
A. Use ln to create hard links because they share the same inode and save space.
B. Use ln -s to create symbolic links because they are easier to manage.
C. Copy the file multiple times to ensure backups are independent.
D. Use ln -s with absolute paths to avoid broken links.

Solution

  1. Step 1: Identify storage-saving method

    Hard links share the same inode and file data, so multiple filenames point to the same data without duplicating storage.
  2. Step 2: Compare with symbolic links and copies

    Symbolic links only point to filenames and do not save storage if the original is deleted. Copying duplicates data, using more space.
  3. Final Answer:

    Use ln to create hard links because they share the same inode and save space. -> Option A
  4. Quick Check:

    Hard links save space by sharing inode [OK]
Hint: Hard links share data, saving space for backups [OK]
Common Mistakes:
  • Choosing symbolic links for storage saving
  • Copying files instead of linking
  • Ignoring inode sharing concept
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard