All Days
Day 14

Claude Code Hooks: Your Safety Net

DAY 15 — 30-DAY SERIES

Claude Code Hooks

Six shell scripts that run automatically to protect your environment, your git history, and your sanity.

6HOOKS
30MIN SETUP
set onceFORGET IT

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.

How hooks work: Claude Code fires hook scripts at defined lifecycle events —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.

Critical — install these first
Nice to have

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

1
Create the hooks directory
mkdir -p ~/.claude/hooks
2
Save each script from the hooks section above

Copy each script and save to ~/.claude/hooks/ with its exact filename.

3
Make all scripts executable
chmod +x ~/.claude/hooks/*.sh
4
Add to ~/.claude/settings.json

Paste the full JSON configuration below into your settings file.

~/.claude/settings.json
{
  "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" }
        ]
      }
    ]
  }
}

don't miss what's next.

playbooks, templates, and tools that actually save you hours. straight to your inbox. no spam. unsubscribe anytime.

Quick reference

All six hooks at a glance.

HookEventMatcherPriorityWhat it does
bash-safety.shPreToolUseBashCRITICALBlocks destructive git and shell commands
file-protection.shPreToolUseEdit|WriteCRITICALBlocks edits to .env and credentials files
file-backup.shPreToolUseEditRECOMMENDEDCreates timestamped backup before every edit
file-logger.shPostToolUseWrite|EditRECOMMENDEDLogs all file changes to daily audit trail
search-year.shPreToolUseWebSearchNICE TO HAVEAppends current year to web searches
stop-notify.shStopNICE TO HAVEPlays sound + sends notification when done
Exit codes
exit 2Blocks the action. Claude stops and reports the error.
exit 0Allows the action. May print a warning first.
Available environment variables
$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...)
Copied