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?