directory-structure-enforcement-hooks

Enforce file placement rules with three-layer Claude Code hooks.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/shimo4228/claude-code-learned-skills --skill directory-structure-enforcement-hooks
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: directory-structure-enforcement-hooks
Source: https://github.com/shimo4228/claude-code-learned-skills/tree/main/skills/directory-structure-enforcement-hooks
Command: npx skills add https://github.com/shimo4228/claude-code-learned-skills --skill directory-structure-enforcement-hooks

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Claude Code drift: prevents files from being created in wrong locations despite CLAUDE.md rules.

Core Features & Use Cases

  • Three-layer defense:
  • PreToolUse hook (docs-prewrite.sh): Write/Edit-time validation, exit 0 to allow, exit 2 to block, stderr to warn. Reads JSON from stdin to inspect tool_input.file_path.
  • Stop hook (driftcheck.sh): session end scan to catch changes by mv or external processes; always exits 0 to avoid blocking the session end.
  • hooks.json matcher exclusion: excludes from generic blockers to avoid conflicts.

Quick Start

Configure and deploy the three-layer hooks to automatically validate file paths during Claude Code changes.

Frequently Asked Questions about directory-structure-enforcement-hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce directory structure rules in Claude Code?

To enforce directory structure in Claude Code, this Skill applies three-layer hooks that intercept file operations, validate paths against rules, and block invalid writes with specific exit codes to prevent drift.

Why does Claude Code put files in the wrong directories despite CLAUDE.md rules?

Claude Code can drift and ignore CLAUDE.md rules. A PreToolUse hook prevents this by intercepting Write and Edit tools, inspecting the file path, and blocking operations with exit code 2 when paths violate structure.

How do I detect directory structure drift caused by external processes or mv commands?

Detect directory drift by running a Stop hook at session end. This hook scans for structural changes caused by mv commands or external processes, always exiting 0 to avoid blocking session termination while reporting deviations.

Can I use PreToolUse hooks to validate file paths without blocking the session?

Yes, PreToolUse hooks can validate file paths and return exit 0 to allow operations, or exit 2 to block them. The Stop hook always exits 0 to avoid blocking the session end during drift detection scans.

What is the best way to prevent file placement drift in source and docs directories?

The best way to prevent file drift is a three-layer defense: PreToolUse validation for writes, a Stop hook for session-end drift detection, and hooks.json matcher exclusion to avoid conflicts with generic blockers.