0
0
Linux CLIscripting~10 mins

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

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