0
0
Linux CLIscripting~5 mins

ln (hard and symbolic links) in Linux CLI - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: ln (hard and symbolic links)
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

Look at what repeats in the commands.

  • Primary operation: Running the ln command once per link.
  • How many times: 100 times for each loop, once per link creation.
How Execution Grows With Input

Each link creation takes about the same time, so total time grows with number of links.

Input Size (n)Approx. Operations
1010 link creations
100100 link creations
10001000 link creations

Pattern observation: The work grows directly with the number of links you create.

Final Time Complexity

Time Complexity: O(n)

This means if you double the number of links, the time to create them roughly doubles too.

Common Mistake

[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.

Interview Connect

Understanding how commands scale with input size shows you think about efficiency, a skill useful in many scripting and automation tasks.

Self-Check

What if we created links in parallel instead of one after another? How would the time complexity change?