Log rotation script in Bash Scripting - Time & Space 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?
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 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.
As the number of log files grows, the script deletes more files beyond the limit.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 deletions (10 - 5) |
| 100 | 95 deletions (100 - 5) |
| 1000 | 995 deletions (1000 - 5) |
Pattern observation: The number of deletions grows linearly as the number of log files increases beyond the limit.
Time Complexity: O(n)
This means the script's work grows in direct proportion to how many extra log files there are to delete.
[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.
Understanding how scripts scale with input size helps you write efficient automation that works well as data grows.
"What if the script compressed old logs instead of deleting them? How would the time complexity change?"