Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Log rotation script in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Log Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this log rotation snippet?
Consider this bash snippet that rotates a log file by renaming it with a timestamp. What will be the output if the current date is 2024-06-15 and the log file exists?
Bash Scripting
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  echo "Rotated $LOGFILE to $BACKUP"
else
  echo "No log file to rotate"
fi
ANo log file to rotate
BRotated app.log to app.log.2024-06-15
CRotated app.log to app.log.$(date +%F)
DError: date command not found
Attempts:
2 left
💡 Hint
Check how the date command is used inside the variable BACKUP.
🔧 Debug
intermediate
2:00remaining
Why does this log rotation script fail to rotate logs?
This script is intended to rotate a log file by compressing the old log and creating a new empty log. Why does it fail to compress the old log?
Bash Scripting
LOGFILE="server.log"
if [ -f "$LOGFILE" ]; then
  gzip $LOGFILE
  > $LOGFILE
  echo "Log rotated and compressed"
else
  echo "No log to rotate"
fi
AThe if condition uses -f which does not detect regular files.
BThe > $LOGFILE line is invalid syntax and causes an error.
CThe gzip command removes the original file, so > $LOGFILE creates a new empty file with the same name, but the script expects the compressed file to exist.
DThe script does not check if gzip is installed, so it fails silently.
Attempts:
2 left
💡 Hint
Think about what gzip does to the original file after compression.
🚀 Application
advanced
3:00remaining
Which script correctly rotates logs keeping only last 5 backups?
You want a bash script that renames the current log file by appending the date, compresses it, and deletes backups older than 5 days. Which script does this correctly?
A
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  find . -name 'app.log.*.gz' -mtime +5 -exec rm -f {} \;
  touch "$LOGFILE"
fi
B
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  find . -name 'app.log.*.gz' -mtime +5 -delete
  touch "$LOGFILE"
fi
C
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  cp "$LOGFILE" "$BACKUP"
  gzip "$LOGFILE"
  find . -name 'app.log.*.gz' -mtime +5 -exec rm {} \;
  > "$LOGFILE"
fi
D
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$LOGFILE"
  find . -name 'app.log.*.gz' -mtime +5 -delete
  touch "$LOGFILE"
fi
Attempts:
2 left
💡 Hint
Check which file is compressed and how old backups are deleted.
📝 Syntax
advanced
2:00remaining
Which option has a syntax error in this log rotation script?
Identify the option that contains a syntax error preventing the script from running.
Bash Scripting
LOGFILE="access.log"
BACKUP="access.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
A
f [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
B
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
C
if
"ELIFGOL$" hcuot  
"PUKCAB$" pizg  
"PUKCAB$" "ELIFGOL$" vm  
neht ;] "ELIFGOL$" f- [ fi
D
if [ -f "$LOGFILE" ] then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
🧠 Conceptual
expert
1:30remaining
What is the main reason to compress rotated logs in automation?
In automated log rotation scripts, why is compressing old log files important?
ATo reduce disk space usage by storing logs in a smaller size
BTo make logs easier to read by humans
CTo prevent logs from being deleted accidentally
DTo speed up the creation of new log files
Attempts:
2 left
💡 Hint
Think about storage and resource management.

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

  1. Step 1: Understand log rotation purpose

    Log rotation scripts archive old logs and create fresh logs to avoid disk space overflow.
  2. 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.
  3. Final Answer:

    To archive old log files and start new ones to prevent disk space issues -> Option A
  4. 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

  1. Step 1: Understand date format for filenames

    Using $(date +%Y-%m-%d) produces a safe filename format like 2024-06-01.
  2. 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.
  3. Final Answer:

    mv logfile.log logfile-$(date +%Y-%m-%d).log -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    3 -> Option D
  4. 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:
mv logfile.log logfile-$(date +%Y-%m-%d).log
gzip logfile-$(date +%Y-%m-%d).log
rm logfile.log
medium
A. The date format uses slashes which are invalid
B. The gzip command is missing the -d option
C. The rm logfile.log command deletes the original log after moving, causing error
D. The mv command should use cp instead

Solution

  1. Step 1: Analyze the commands order

    The mv command renames logfile.log to logfile-date.log, so logfile.log no longer exists after this.
  2. 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.
  3. Final Answer:

    The rm logfile.log command deletes the original log after moving, causing error -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    ls -1t logfile-*.log.gz | tail -n +6 | xargs rm -f -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard