Bird
Raised Fist0
Linux CLIscripting~10 mins

ln (hard and symbolic links) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ln (hard and symbolic links)
Start with original file
↓
Choose link type: hard or symbolic
↓
Create hard link
↓
Link points directly to file inode
↓
Use link as file reference
↓
Changes affect original and hard link
↓
Symbolic link broken if original removed
This flow shows how creating hard or symbolic links connects files differently: hard links share the same data, symbolic links point to file paths.
Execution Sample
Linux CLI
echo 'Hello' > file.txt
ln file.txt hardlink.txt
ln -s file.txt symlink.txt
cat hardlink.txt
cat symlink.txt
Creates a file, then a hard link and a symbolic link to it, then reads both links.
Execution Table
StepCommandActionResult/Output
1echo 'Hello' > file.txtCreate file.txt with content 'Hello'No output
2ln file.txt hardlink.txtCreate hard link hardlink.txt to file.txtNo output
3ln -s file.txt symlink.txtCreate symbolic link symlink.txt pointing to file.txtNo output
4cat hardlink.txtRead content via hardlink.txtHello
5cat symlink.txtRead content via symlink.txtHello
💡 All commands executed successfully; links created and content read.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
file.txt contentnoneHelloHelloHelloHelloHello
hardlink.txt contentnonenoneHelloHelloHelloHello
symlink.txt targetnonenonenonefile.txtfile.txtfile.txt
Key Moments - 3 Insights
Why does cat on hardlink.txt and symlink.txt both show 'Hello'?
Because hardlink.txt points directly to the same data as file.txt, and symlink.txt points to file.txt path, both access the same content (see execution_table steps 4 and 5).
What happens if file.txt is deleted?
Hardlink.txt still accesses the data because it points to the inode directly, but symlink.txt breaks because it points to the file path which no longer exists.
Why does ln -s create a symbolic link instead of a hard link?
The -s option tells ln to create a symbolic link, which stores the file path, unlike a hard link which shares the inode (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of hardlink.txt after step 2?
AHello
Bnone
Cfile.txt
DError
💡 Hint
Check the 'Result/Output' column at step 2 and variable_tracker for hardlink.txt content.
At which step is the symbolic link created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Command' column for ln -s command in execution_table.
If file.txt is deleted after step 5, what happens when you cat symlink.txt?
AOutputs 'Hello'
BOutputs nothing but no error
CShows an error 'No such file or directory'
DDeletes symlink.txt automatically
💡 Hint
Recall that symbolic links point to file paths; if the target is missing, cat fails (see key_moments).
Concept Snapshot
ln command creates links to files.
Hard links share the same data (inode).
Symbolic links point to file paths.
Hard links remain if original deleted.
Symbolic links break if target removed.
Use ln -s for symbolic links.
Full Transcript
This lesson shows how the ln command creates hard and symbolic links in Linux. First, a file named file.txt is created with the text 'Hello'. Then, a hard link named hardlink.txt is made, which points directly to the same data as file.txt. Next, a symbolic link named symlink.txt is created, which points to the path of file.txt. Reading either hardlink.txt or symlink.txt outputs 'Hello' because both links access the same content. Hard links share the inode, so if the original file is deleted, the hard link still works. Symbolic links store the file path, so if the original file is deleted, the symbolic link breaks and shows an error. This visual trace helps understand the difference and behavior of hard and symbolic links.

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