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

Clean up files older than two days in `/tmp/app-cache`, owned by `app`, where filenames may contain spaces and newlines.

BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → Let find select and delete; no parsing of names is needed.

What I'd do →

find /tmp/app-cache -xdev -type f -user app -mtime +2 -print -delete
find /tmp/app-cache -xdev -mindepth 1 -type d -empty -delete
  1. -xdev stays on one filesystem; -print logs what was deleted.
  2. -delete handles any filename because no shell or xargs splitting is involved.
  3. Alternatively, declare it with systemd-tmpfiles: d /tmp/app-cache 0750 app app 2d in /etc/tmpfiles.d/app.conf.

Why → The safest way to handle weird filenames is to never turn them into text.

Interview tip → Mention systemd-tmpfiles; many teams forget it exists and write cron jobs instead.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping