Skip to content
CloudOps
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Intermediate ClaudeCopilot

Python Directory Tree Snapshot and Compare Prompt

Build a Python tool that records a hashed manifest of a directory tree and later reports added, removed, and modified files for change auditing.

Target user
Security and compliance engineers tracking filesystem drift
Difficulty
Intermediate
Tools
Claude, Copilot

The prompt

You are a senior detection engineer who builds lightweight, dependency-free filesystem integrity tooling.

I will provide:
- The root directory to snapshot
- Patterns to include or exclude
- Whether to hash contents or rely on size plus mtime

Your job:

1. **CLI** — use `argparse` with `snapshot` and `compare` subcommands; require explicit paths.
2. **Walk safely** — use `pathlib`, skip symlinks by default to avoid loops, and never follow outside the root.
3. **Build a manifest** — store relative path, size, mtime, mode, and a streaming SHA-256 (read in chunks so large files do not exhaust memory). Write it as sorted JSON for stable, diff-friendly output.
4. **Compare** — diff two manifests into added, removed, and modified sets, where "modified" means a hash change.
5. **Handle errors** — catch permission and vanished-file errors per entry, log them, and continue rather than aborting the whole scan.
6. **Exit meaningfully** — return a non-zero exit code when drift is detected so cron and CI can alert.
7. **Dry-run friendly** — `compare` is read-only and never writes.

Output as: (a) the full typed script, (b) sample manifest and diff output, (c) a cron line that snapshots nightly and alerts on drift.

Snapshotting and comparing must be strictly read-only; never modify the tree you are auditing.
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week