What if your server crashes because you forgot to clean logs? A log rotation script can save you from that nightmare.
Why Log rotation script in Bash Scripting? - Purpose & Use Cases
Imagine you have a busy website or app that creates log files every day. These logs keep growing and growing, filling up your disk space. You try to clean them up by hand, opening folders, deleting old files, and renaming others.
Doing this manually is slow and boring. You might forget to delete some files or accidentally remove important logs. It's easy to make mistakes, and if you miss cleaning logs on time, your system can run out of space and crash.
A log rotation script automatically manages your log files. It renames old logs, compresses them to save space, and deletes the oldest ones. This keeps your system tidy without you lifting a finger.
rm /var/log/app.log.1 mv /var/log/app.log /var/log/app.log.1
logrotate /etc/logrotate.d/app
With log rotation scripts, your system stays healthy and you save time, avoiding manual cleanup and unexpected crashes.
Web servers like Apache or Nginx generate huge logs daily. Using log rotation scripts, these logs are automatically archived and cleaned, so the server never runs out of disk space.
Manual log cleanup is slow and risky.
Log rotation scripts automate archiving and deleting old logs.
This keeps systems stable and saves your time.
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
