Log rotation script
📖 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