Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Data rotation and cleanup in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of log file rotation script

What is the output of this Raspberry Pi shell script that rotates a log file by renaming and compressing it?

Raspberry Pi
#!/bin/bash
LOGFILE="/var/log/myapp.log"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$LOGFILE.$(date +%Y%m%d)"
  gzip "$LOGFILE.$(date +%Y%m%d)"
  echo "Log rotated"
else
  echo "No log to rotate"
fi
Amv: cannot stat '/var/log/myapp.log': No such file or directory
BLog rotated
CNo log to rotate
Dgzip: command not found
Attempts:
2 left
💡 Hint

Assume the log file exists before running the script.

🧠 Conceptual
intermediate
1:30remaining
Understanding log cleanup with find command

Which find command will delete all compressed log files older than 7 days in /var/log?

Afind /var/log -name '*.gz' -mtime +7 -delete
Bfind /var/log -name '*.gz' -mtime -7 -delete
Cfind /var/log -name '*.gz' -mtime 7 -delete
Dfind /var/log -name '*.gz' -mtime +7 -exec rm {} \;
Attempts:
2 left
💡 Hint

Remember -mtime +7 means files modified more than 7 days ago.

🔧 Debug
advanced
2:00remaining
Fix the Python script for rotating logs

What error does this Python script raise when trying to rotate logs?

import os
import datetime
logfile = '/var/log/myapp.log'
if os.path.exists(logfile):
    newname = logfile + datetime.datetime.now().strftime('%Y%m%d')
    os.rename(logfile, newname)
    os.system(f'gzip {newname}')
    print('Rotated')
else:
    print('No log')
APermissionError
BFileNotFoundError
CSyntaxError
DNo error, prints 'Rotated'
Attempts:
2 left
💡 Hint

Consider typical permissions for /var/log on Raspberry Pi.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this log cleanup script

Which option contains the syntax error in this Bash snippet?

for file in /var/log/*.gz
 do
  if [ $(stat -c %Y "$file") -lt $(date -d '7 days ago' +%s) ]; then
    rm "$file"
  fi
 done
AMissing semicolon after for loop declaration
BMissing closing bracket in if condition
CNo syntax error
DIncorrect use of single quotes in date command
Attempts:
2 left
💡 Hint

Check the syntax of the for loop header in Bash.

🚀 Application
expert
2:00remaining
Determine the number of rotated log files after cleanup

A Raspberry Pi rotates logs daily by renaming myapp.log to myapp.log.YYYYMMDD and compressing it. The cleanup script deletes compressed logs older than 10 days. If logs have been rotated daily for 15 days, how many compressed log files remain after running cleanup?

A0
B5
C15
D10
Attempts:
2 left
💡 Hint

Think about which files are deleted and which remain based on age.

Practice

(1/5)
1. What is the main purpose of data rotation and cleanup on a Raspberry Pi?
easy
A. To keep storage organized and remove old files
B. To increase the CPU speed
C. To install new software automatically
D. To change the screen resolution

Solution

  1. Step 1: Understand data rotation and cleanup

    Data rotation and cleanup help manage files by removing old or unnecessary data.
  2. Step 2: Identify the main goal

    The goal is to keep storage organized and free space by deleting old files.
  3. Final Answer:

    To keep storage organized and remove old files -> Option A
  4. Quick Check:

    Data rotation = storage cleanup [OK]
Hint: Think about storage space and file management [OK]
Common Mistakes:
  • Confusing data cleanup with hardware upgrades
  • Thinking it changes system performance directly
  • Assuming it installs software automatically
2. Which of the following commands correctly deletes files older than 7 days in the /var/log directory on Raspberry Pi?
easy
A. find /var/log -type d -mtime +7 -delete
B. find /var/log -type f -mtime +7 -delete
C. rm /var/log/* -mtime +7
D. delete /var/log -older 7

Solution

  1. Step 1: Understand the find command syntax

    To delete files older than 7 days, use find with -type f (files), -mtime +7 (older than 7 days), and -delete.
  2. Step 2: Check each option

    find /var/log -type f -mtime +7 -delete uses correct syntax. find /var/log -type d -mtime +7 -delete targets directories (-type d), not files. rm /var/log/* -mtime +7 uses rm incorrectly with -mtime. delete /var/log -older 7 is not a valid command.
  3. Final Answer:

    find /var/log -type f -mtime +7 -delete -> Option B
  4. Quick Check:

    find + -mtime +7 + -delete = correct [OK]
Hint: Use find with -type f and -mtime +days to delete old files [OK]
Common Mistakes:
  • Using -type d instead of -type f
  • Trying to use rm with -mtime option
  • Using invalid commands like delete
3. What will be the output of this script snippet on Raspberry Pi?
logfile="/var/log/syslog"
backup="/var/log/syslog-$(date +%Y%m%d)"

mv $logfile $backup
> $logfile
ls /var/log/syslog*
medium
A. Only /var/log/syslog-YYYYMMDD file listed
B. Only /var/log/syslog file listed
C. /var/log/syslog and /var/log/syslog-YYYYMMDD files listed
D. No files listed, error occurs

Solution

  1. Step 1: Understand the script actions

    The script renames syslog to syslog-YYYYMMDD (today's date), then creates a new empty syslog file.
  2. Step 2: Predict the ls output

    After these steps, both /var/log/syslog (empty) and /var/log/syslog-YYYYMMDD exist, so ls lists both.
  3. Final Answer:

    /var/log/syslog and /var/log/syslog-YYYYMMDD files listed -> Option C
  4. Quick Check:

    mv + new empty file + ls = both files [OK]
Hint: Renaming then recreating file shows both in listing [OK]
Common Mistakes:
  • Thinking syslog file is deleted
  • Assuming mv copies instead of moves
  • Ignoring the empty file creation with >
4. Identify the error in this Raspberry Pi log rotation script snippet:
logfile="/var/log/app.log"
backup="/var/log/app.log-$(date +%Y%m%d)"

mv $logfile $backup
> $logfile
find /var/log -type f -mtime 7 -delete
medium
A. The find command should use -mtime +7 to delete files older than 7 days
B. The mv command syntax is incorrect
C. The > $logfile line should be before mv command
D. The backup filename format is invalid

Solution

  1. Step 1: Check the find command usage

    The find command uses -mtime 7 which matches files exactly 7 days old, not older than 7 days.
  2. Step 2: Correct the find command

    To delete files older than 7 days, use -mtime +7 instead of -mtime 7.
  3. Final Answer:

    The find command should use -mtime +7 to delete files older than 7 days -> Option A
  4. Quick Check:

    find -mtime +7 deletes older files [OK]
Hint: Use + before days in -mtime to delete older files [OK]
Common Mistakes:
  • Using -mtime 7 deletes only files exactly 7 days old
  • Misplacing the > $logfile line
  • Incorrect mv command syntax
5. You want to automate log rotation on your Raspberry Pi so that every day the current log file /var/log/myapp.log is renamed with the date, a new empty log is created, and logs older than 14 days are deleted. Which script snippet correctly performs this task?
hard
A. mv /var/log/myapp.log /var/log/myapp.log-$(date +%Y%m%d) > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime 14 -delete
B. mv /var/log/myapp.log /var/log/myapp.log-$(date +%Y%m%d) rm /var/log/myapp.log find /var/log -type f -mtime +14 -delete
C. backup="/var/log/myapp.log-$(date +%Y%m%d)" cp /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime 14 -delete
D. backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete

Solution

  1. Step 1: Rename current log and create new empty log

    backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete correctly moves the log file to a dated backup and creates a new empty log with >.
  2. Step 2: Delete logs older than 14 days

    backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete uses find with -name 'myapp.log-*' and -mtime +14 to delete backups older than 14 days, which is correct.
  3. Final Answer:

    backup="/var/log/myapp.log-$(date +%Y%m%d)" mv /var/log/myapp.log $backup > /var/log/myapp.log find /var/log -type f -name 'myapp.log-*' -mtime +14 -delete -> Option D
  4. Quick Check:

    mv + new file + find -mtime +14 = correct rotation [OK]
Hint: Move log, recreate empty, delete backups older than 14 days [OK]
Common Mistakes:
  • Using rm instead of recreating empty log
  • Using -mtime 14 instead of +14 in find
  • Copying instead of moving log file
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