📖 Scenario: You have a file named report.txt in your home directory. You want to create two types of links to this file: a hard link and a symbolic link. This will help you understand how links work in Linux.
🎯 Goal: Create a hard link named report_hard.txt and a symbolic link named report_sym.txt to the original report.txt file using the ln command.
📋 What You'll Learn
Create a file named report.txt with the exact content: Monthly sales report
Create a hard link named report_hard.txt pointing to report.txt
Create a symbolic link named report_sym.txt pointing to report.txt
Display the list of files with details to show the links
💡 Why This Matters
🌍 Real World
Creating hard and symbolic links is common when managing files and backups in Linux systems. It helps save space and organize files efficiently.
💼 Career
Understanding links is important for system administrators, developers, and anyone working with Linux servers or file systems.
Progress0 / 4 steps
1
Create the original file
Create a file called report.txt with the exact content Monthly sales report using the echo command and output redirection.
Linux CLI
Hint
Use echo "Monthly sales report" > report.txt to create the file with content.
2
Create a hard link
Create a hard link named report_hard.txt pointing to the existing file report.txt using the ln command.
Linux CLI
Hint
Use ln report.txt report_hard.txt to create a hard link.
3
Create a symbolic link
Create a symbolic link named report_sym.txt pointing to the existing file report.txt using the ln -s command.
Linux CLI
Hint
Use ln -s report.txt report_sym.txt to create a symbolic link.
4
List files to verify links
Use the ls -li command to list the files report.txt, report_hard.txt, and report_sym.txt with inode numbers and details to verify the links.
Linux CLI
Hint
Use ls -li report.txt report_hard.txt report_sym.txt to see inode numbers and link details.
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
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.
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.
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
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
Step 1: Recall symbolic link syntax
The syntax for creating a symbolic link is ln -s <target> <link_name>.
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.
Final Answer:
ln -s originalfile linkfile -> Option A
Quick Check:
ln -s target link_name [OK]
Hint: Use ln -s target link_name for symbolic links [OK]
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
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".
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.
Final Answer:
file2: Hello file3: cat: file3: No such file or directory -> Option D
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
Step 1: Understand the error cause
The error occurs because linkname already exists, and ln refuses to overwrite existing files or links by default.
Step 2: Use force option to overwrite
Using ln -sf forces the symbolic link creation by overwriting the existing linkname safely.
Final Answer:
Use ln -sf /path/to/file linkname to force overwrite the existing link. -> Option B
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
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.
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.
Final Answer:
Use ln to create hard links because they share the same inode and save space. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.