Claude Code Hooks
Six shell scripts that run automatically to protect your environment, your git history, and your sanity.
Why hooks matter
Claude Code can run shell commands, edit files, and interact with git on your behalf. That's incredibly powerful — and without guardrails, incredibly risky.
Automatic protection
Hooks fire before and after every tool use. They block dangerous commands before they run, not after.
Silent guardrails
Scripts run in the background. You don't see them unless something gets blocked. Set once, forget it.
Full audit trail
Every file change gets logged with a timestamp. See exactly what Claude touched and when.
One-click restore
Timestamped backups before every edit. The last 10 versions of each file, always available.
PreToolUse, PostToolUse, and Stop. Each script receives context via environment variables like $CLAUDE_TOOL_INPUT and $CLAUDE_FILE_PATH. Exit code 2 blocks the action; exit code 0 allows it.The 6 hooks
Click any hook to expand the full script. Copy and save to your hooks directory.
Activate all hooks in settings.json
Add this configuration to your ~/.claude/settings.json to wire up all six hooks. After saving, run: chmod +x ~/.claude/hooks/*.sh
mkdir -p ~/.claude/hooks
Copy each script and save to ~/.claude/hooks/ with its exact filename.
chmod +x ~/.claude/hooks/*.sh
Paste the full JSON configuration below into your settings file.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/bash-safety.sh" }
]
},
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/file-protection.sh" }
]
},
{
"matcher": "Edit",
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/file-backup.sh" }
]
},
{
"matcher": "WebSearch",
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/search-year.sh" }
]
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/file-logger.sh" }
]
}
],
"Stop": [
{
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/stop-notify.sh" }
]
}
]
}
}Quick reference
All six hooks at a glance.
| Hook | Event | Matcher | Priority | What it does |
|---|---|---|---|---|
bash-safety.sh | PreToolUse | Bash | CRITICAL | Blocks destructive git and shell commands |
file-protection.sh | PreToolUse | Edit|Write | CRITICAL | Blocks edits to .env and credentials files |
file-backup.sh | PreToolUse | Edit | RECOMMENDED | Creates timestamped backup before every edit |
file-logger.sh | PostToolUse | Write|Edit | RECOMMENDED | Logs all file changes to daily audit trail |
search-year.sh | PreToolUse | WebSearch | NICE TO HAVE | Appends current year to web searches |
stop-notify.sh | Stop | — | NICE TO HAVE | Plays sound + sends notification when done |
exit 2Blocks the action. Claude stops and reports the error.exit 0Allows the action. May print a warning first.$CLAUDE_TOOL_INPUTThe full command or query being run$CLAUDE_FILE_PATHAbsolute path of the file being edited$CLAUDE_TOOL_NAMEName of the tool being used (Bash, Edit, Write...)