Interview Prep/Shell Scripting/Scenarios: backups, log rotation and housekeeping

An app writes `app-YYYY-MM-DD.log` files daily. There is no logrotate. Write the housekeeping.

BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → Compress yesterday's and older files, delete old compressed files, and never touch today's file.

What I'd do →

dir=/var/log/app
today="app-$(date +%F).log"
find "$dir" -maxdepth 1 -type f -name 'app-*.log' ! -name "$today" -mmin +60 -exec gzip -9 -- {} +
find "$dir" -maxdepth 1 -type f -name 'app-*.log.gz' -mtime +30 -delete
  1. ! -name "$today" and -mmin +60 protect the active file.
  2. Schedule daily at 00:15 via a timer.

Why → Date-named files need no rename or signal; compression and deletion by age are enough.

Interview tip → Mention that logrotate can still do this with dateext patterns, but a two-line find is often clearer.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping