ln (hard and symbolic links) in Linux CLI - Time & Space Complexity
When creating links in Linux, it helps to know how the time to create them changes as you make more links.
We want to see how the command's work grows when making many links.
Analyze the time complexity of the following commands creating links.
# Create 100 hard links to a file
for i in $(seq 1 100); do
ln original.txt hardlink_$i.txt
done
# Create 100 symbolic links to a file
for i in $(seq 1 100); do
ln -s original.txt symlink_$i.txt
done
This code creates many hard and symbolic links to the same original file.
Look at what repeats in the commands.
- Primary operation: Running the
lncommand once per link. - How many times: 100 times for each loop, once per link creation.
Each link creation takes about the same time, so total time grows with number of links.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 link creations |
| 100 | 100 link creations |
| 1000 | 1000 link creations |
Pattern observation: The work grows directly with the number of links you create.
Time Complexity: O(n)
This means if you double the number of links, the time to create them roughly doubles too.
[X] Wrong: "Creating many symbolic links is faster than hard links because they are just shortcuts."
[OK] Correct: Both types require a similar amount of work per link; the difference in speed is usually very small and depends on the file system, not the link type.
Understanding how commands scale with input size shows you think about efficiency, a skill useful in many scripting and automation tasks.
What if we created links in parallel instead of one after another? How would the time complexity change?
Practice
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 CQuick Check:
Hard link = inode sharing, Symbolic link = filename pointer [OK]
- Confusing which link shares inode
- Thinking symbolic links share file data
- Mixing up commands for creating links
linkfile pointing to originalfile?Solution
Step 1: Recall symbolic link syntax
The syntax for creating a symbolic link isln -s <target> <link_name>.Step 2: Match command to syntax
ln -s originalfile linkfile usesln -s originalfile linkfile, which correctly creates a symbolic link namedlinkfilepointing tooriginalfile.Final Answer:
ln -s originalfile linkfile -> Option AQuick Check:
ln -s target link_name [OK]
- Swapping target and link name order
- Omitting -s for symbolic links
- Using ln -s with wrong argument order
echo "Hello" > file1 ln file1 file2 ln -s file1 file3 rm file1
What will be the output of
cat file2 and cat file3?Solution
Step 1: Understand hard link behavior after deleting original
Hard linkfile2points to the same inode asfile1. Removingfile1does not delete the data becausefile2still references it. Socat file2outputs "Hello".Step 2: Understand symbolic link behavior after deleting original
Symbolic linkfile3points to the namefile1. After deletingfile1,file3points to a non-existent file, socat file3gives an error.Final Answer:
file2: Hello
file3: cat: file3: No such file or directory -> Option DQuick Check:
Hard link survives deletion; symbolic link breaks [OK]
- Assuming symbolic link still works after target deletion
- Thinking hard link deletes data with original
- Confusing link types' behavior on file removal
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?Solution
Step 1: Understand the error cause
The error occurs becauselinknamealready exists, andlnrefuses to overwrite existing files or links by default.Step 2: Use force option to overwrite
Usingln -sfforces the symbolic link creation by overwriting the existinglinknamesafely.Final Answer:
Use ln -sf /path/to/file linkname to force overwrite the existing link. -> Option BQuick Check:
ln -sf overwrites existing links safely [OK]
- Deleting the target file instead of the link
- Omitting -s and creating hard link by mistake
- Renaming target unnecessarily
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 AQuick Check:
Hard links save space by sharing inode [OK]
- Choosing symbolic links for storage saving
- Copying files instead of linking
- Ignoring inode sharing concept
Ready to prove it?
Pass the test and this pattern moves unknown
