📖 Scenario: You manage a server that creates a log file called app.log. Over time, this file grows large and needs to be rotated to keep the system tidy and save space.Rotating logs means renaming the current log file to keep it as a backup and starting a fresh empty log file for new entries.
🎯 Goal: Create a simple bash script that rotates the app.log file by renaming it with a numbered suffix and then creating a new empty app.log file.
📋 What You'll Learn
Create a variable for the log file name app.log.
Create a variable for the maximum number of backups to keep, set to 3.
Write a loop to rename existing backup files by increasing their number suffix by 1, starting from the highest number.
Rename the current app.log to app.log.1.
Create a new empty app.log file.
Print a message confirming the rotation.
💡 Why This Matters
🌍 Real World
Log rotation is a common task in system administration to keep log files manageable and prevent disk space issues.
💼 Career
Knowing how to automate log rotation with scripts is useful for junior sysadmins, DevOps engineers, and anyone managing servers.
Progress0 / 4 steps
1
Set up the log file variable
Create a variable called logfile and set it to the string app.log.
Bash Scripting
Hint
Use logfile="app.log" to create the variable.
2
Set the maximum number of backups
Add a variable called max_backups and set it to 3.
Bash Scripting
Hint
Use max_backups=3 to set the number.
3
Rename existing backups with a loop
Write a for loop that counts down from max_backups to 1. Inside the loop, rename each existing backup file app.log.N to app.log.N+1 if it exists. Use mv command and check if the file exists with [ -f filename ].
Bash Scripting
Hint
Use a C-style for loop: for (( i=max_backups; i>=1; i-- )) and inside check if the file exists before moving it.
4
Rotate current log and create new empty log
Rename the current app.log to app.log.1 using mv. Then create a new empty app.log file using touch. Finally, print the message "Log rotated successfully." using echo.
Bash Scripting
Hint
Use mv "$logfile" "$logfile.1" to rename the current log, touch "$logfile" to create a new empty file, and echo to print the message.
Practice
(1/5)
1. What is the main purpose of a log rotation script in bash?
easy
A. To archive old log files and start new ones to prevent disk space issues
B. To delete all log files permanently
C. To create new log files without removing old ones
D. To convert log files into executable scripts
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 A
Quick Check:
Log rotation = archive old + new logs [OK]
Hint: Log rotation means archiving old logs and starting fresh ones [OK]
Common Mistakes:
Thinking log rotation deletes all logs
Believing it only creates new logs without archiving
Confusing log rotation with script conversion
2. Which of the following bash commands correctly appends the current date to a log filename for rotation?
easy
A. mv logfile.log logfile-date.log
B. mv logfile.log logfile-$(date +%Y-%m-%d).log
C. mv logfile.log logfile-$(date +%H:%M:%S).log
D. mv logfile.log logfile-$(date +%Y/%m/%d).log
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 B
Quick Check:
Date format with dashes = safe filename [OK]
Hint: Use date with dashes, avoid colons or slashes in filenames [OK]
Common Mistakes:
Using colons or slashes in filenames
Not using command substitution for date
Using literal text instead of date command
3. Given this snippet in a log rotation script:
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?
medium
A. Error: No such file or directory
B. 0
C. 1
D. 3
Solution
Step 1: Understand the commands
The gzip command compresses a log file, creating a file ending with .gz. The ls logfile-*.log.gz lists all compressed log files matching the pattern.
Step 2: Count matching files
If there are 3 compressed log files, ls outputs 3 lines. Piping to wc -l counts these lines, so output is 3.
Final Answer:
3 -> Option D
Quick Check:
Count of compressed logs = 3 [OK]
Hint: Count lines from ls output to get number of files [OK]
Common Mistakes:
Assuming gzip outputs count
Thinking wc -l counts characters
Expecting error if files exist
4. Identify the error in this log rotation snippet:
C. The rm logfile.log command deletes the original log after moving, causing error
D. The mv command should use cp instead
Solution
Step 1: Analyze the commands order
The mv command renames logfile.log to logfile-date.log, so logfile.log no longer exists after this.
Step 2: Check the rm command
After moving, rm logfile.log tries to delete a file that was already moved, causing an error or no effect.
Final Answer:
The rm logfile.log command deletes the original log after moving, causing error -> Option C
Quick Check:
Deleting moved file causes error [OK]
Hint: Don't delete a file after moving it; it's already renamed [OK]
Common Mistakes:
Deleting a file after moving it
Confusing gzip options
Using invalid date formats
5. You want to write a log rotation script that keeps only the last 5 compressed log backups and deletes older ones. Which bash command snippet correctly deletes older backups?
hard
A. ls -1t logfile-*.log.gz | tail -n +6 | xargs rm -f
B. ls -1t logfile-*.log.gz | head -n +6 | xargs rm -f
C. ls -1 logfile-*.log.gz | tail -n 5 | xargs rm -f
D. ls logfile-*.log.gz | grep -v 'logfile-*.log.gz' | xargs rm -f
Solution
Step 1: Understand the command parts
ls -1t lists files sorted by modification time, newest first. tail -n +6 outputs lines starting from line 6, i.e., files older than the 5 newest. xargs rm -f deletes 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 A
Quick Check:
Keep 5 newest, delete older = tail from 6th [OK]
Hint: Use ls -t and tail +6 to delete older files beyond 5 [OK]
Common Mistakes:
Using head instead of tail to select files
Deleting newest files instead of oldest
Not sorting files by modification time
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.