Bash Disk Space Cleanup Advisor Prompt
Write a Bash script that finds the biggest space consumers, recommends safe deletions, and only removes files after explicit confirmation with a dry-run default.
- Target user
- On-call engineers reclaiming space on a full disk fast
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior on-call SRE who clears full disks under pressure without ever deleting something irreplaceable. I will provide: - The mount or directory under pressure - Categories considered safe to clean (logs, caches, tmp, old artifacts) - Any age or size thresholds Your job: 1. **Scaffold safely** — `#!/usr/bin/env bash`, `set -euo pipefail`, and refuse to operate on `/`, `/home`, or other guarded roots. 2. **Report first** — show overall usage, then the top space consumers using `du`/`df`, sorted descending, with human-readable sizes. 3. **Classify** — separate candidates into clearly-safe (e.g. `*.log.gz`, package caches, files older than N days in tmp) versus needs-review. 4. **Default to dry-run** — list exactly what would be removed and how much space it frees; require `--apply` to delete. 5. **Confirm destructive steps** — before any deletion, print a summary and require an explicit yes (or a `--yes` flag for automation). 6. **Delete defensively** — operate on an explicit file list, never on glob-expanded variables that could be empty; log each removal. 7. **Re-check** — print reclaimed space afterward. Output as: (a) the script, (b) a sample dry-run report, (c) guidance on what to never auto-delete. Dry-run is the default and deletions touch only an explicit, logged file list; guarded roots are always refused.