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?"
Practice
log rotation script in bash?Solution
Step 1: Understand log rotation purpose
Log rotation scripts archive old logs and create fresh logs to avoid disk space overflow.Step 2: Compare options
Only To archive old log files and start new ones to prevent disk space issues describes archiving old logs and starting new ones, which matches the purpose.Final Answer:
To archive old log files and start new ones to prevent disk space issues -> Option AQuick Check:
Log rotation = archive old + new logs [OK]
- Thinking log rotation deletes all logs
- Believing it only creates new logs without archiving
- Confusing log rotation with script conversion
Solution
Step 1: Understand date format for filenames
Using$(date +%Y-%m-%d)produces a safe filename format like 2024-06-01.Step 2: Check each option
mv logfile.log logfile-$(date +%Y-%m-%d).log uses dashes which are safe in filenames. mv logfile.log logfile-date.log uses literal 'date', not dynamic. mv logfile.log logfile-$(date +%H:%M:%S).log uses colons which are invalid in filenames on many systems. mv logfile.log logfile-$(date +%Y/%m/%d).log uses slashes which are directory separators, invalid in filenames.Final Answer:
mv logfile.log logfile-$(date +%Y-%m-%d).log -> Option BQuick Check:
Date format with dashes = safe filename [OK]
- Using colons or slashes in filenames
- Not using command substitution for date
- Using literal text instead of date command
gzip logfile-$(date +%Y-%m-%d).log ls logfile-*.log.gz | wc -l
What will the
wc -l command output if there are 3 compressed log files matching the pattern?Solution
Step 1: Understand the commands
Thegzipcommand compresses a log file, creating a file ending with.gz. Thels logfile-*.log.gzlists all compressed log files matching the pattern.Step 2: Count matching files
If there are 3 compressed log files,lsoutputs 3 lines. Piping towc -lcounts these lines, so output is 3.Final Answer:
3 -> Option DQuick Check:
Count of compressed logs = 3 [OK]
- Assuming gzip outputs count
- Thinking wc -l counts characters
- Expecting error if files exist
mv logfile.log logfile-$(date +%Y-%m-%d).log gzip logfile-$(date +%Y-%m-%d).log rm logfile.log
Solution
Step 1: Analyze the commands order
Themvcommand renames logfile.log to logfile-date.log, so logfile.log no longer exists after this.Step 2: Check the
After moving,rmcommandrm logfile.logtries to delete a file that was already moved, causing an error or no effect.Final Answer:
Therm logfile.logcommand deletes the original log after moving, causing error -> Option CQuick Check:
Deleting moved file causes error [OK]
- Deleting a file after moving it
- Confusing gzip options
- Using invalid date formats
Solution
Step 1: Understand the command parts
ls -1tlists files sorted by modification time, newest first.tail -n +6outputs lines starting from line 6, i.e., files older than the 5 newest.xargs rm -fdeletes those files.Step 2: Check other options
ls -1t logfile-*.log.gz | head -n +6 | xargs rm -f deletes the first 6 newest files (wrong). ls -1 logfile-*.log.gz | tail -n 5 | xargs rm -f deletes last 5 files by name order, not time. ls logfile-*.log.gz | grep -v 'logfile-*.log.gz' | xargs rm -f uses grep incorrectly and does not select older files.Final Answer:
ls -1t logfile-*.log.gz | tail -n +6 | xargs rm -f -> Option AQuick Check:
Keep 5 newest, delete older = tail from 6th [OK]
- Using head instead of tail to select files
- Deleting newest files instead of oldest
- Not sorting files by modification time
Ready to prove it?
Pass the test and this pattern moves unknown
