Interview Prep/Linux/Scheduling, automation and scripting

A cleanup script must delete files older than 14 days in `/data/exports`, but someone once deleted `/` by mistake with a similar script. How do you write it safely?

IntermediateScenarioScheduling, automation and scripting
Answer

Situation read → Guard against empty variables and wrong paths.

What I'd do →

#!/usr/bin/env bash
set -euo pipefail
target="${EXPORT_DIR:?EXPORT_DIR not set}"
[[ "$target" == /data/exports* ]] || { echo "refusing: $target"; exit 1; }
find "$target" -xdev -type f -mtime +14 -print -delete

Run once with -print only (no -delete) to review.

Why → ${VAR:?} aborts if empty, so rm -rf "$VAR/" can never become rm -rf /. -xdev stops find crossing into other mounts.

Interview tip → Mention the famous Steam bug (rm -rf "$STEAMROOT/"* with an empty variable).

All Linux questions Roadmap: Linux

More on Scheduling, automation and scripting