0
0
Bash Scriptingscripting~5 mins

Log rotation script in Bash Scripting - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Log rotation script
O(n)
Understanding Time Complexity

We want to understand how the time needed to run a log rotation script changes as the number of log files grows.

Specifically, how does the script's work increase when there are more logs to rotate?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#!/bin/bash
LOG_DIR="/var/log/myapp"
MAX_FILES=5

files=($(ls -t "$LOG_DIR"/*.log))

for (( i=$MAX_FILES; i<${#files[@]}; i++ )); do
  rm "${files[$i]}"
done

This script lists log files sorted by newest first, then deletes older logs beyond a set limit.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Loop over log files starting from the (MAX_FILES)th file to delete old logs.
  • How many times: The loop runs once for each log file beyond the maximum allowed, which depends on total log count minus MAX_FILES.
How Execution Grows With Input

As the number of log files grows, the script deletes more files beyond the limit.

Input Size (n)Approx. Operations
105 deletions (10 - 5)
10095 deletions (100 - 5)
1000995 deletions (1000 - 5)

Pattern observation: The number of deletions grows linearly as the number of log files increases beyond the limit.

Final Time Complexity

Time Complexity: O(n)

This means the script's work grows in direct proportion to how many extra log files there are to delete.

Common Mistake

[X] Wrong: "The script always takes the same time because MAX_FILES is fixed."

[OK] Correct: Even though MAX_FILES is fixed, the script deletes all files beyond that number, so if there are many extra logs, it takes longer.

Interview Connect

Understanding how scripts scale with input size helps you write efficient automation that works well as data grows.

Self-Check

"What if the script compressed old logs instead of deleting them? How would the time complexity change?"