Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
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.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Related prompts

More Bash & Python Automation prompts & error guides

Browse every Bash & Python Automation prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.