Back to Hooks

file-backup

DevelopmentยทPreToolUse

Automatically backup files before editing

Matcher: Edit|MultiEdit

Configuration

Add to your .claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "hooks": [
          {
            "type": "command",
            "command": ".claude/hooks/file-backup.sh"
          }
        ],
        "matcher": "Edit|MultiEdit"
      }
    ]
  }
}

This hook includes a script file. Download the ZIP bundle for complete installation with script and instructions.

Hook Details


file-backup


Automatically backup files before editing


Event Configuration


  • **Event Type**: `PreToolUse`
  • **Tool Matcher**: `Edit|MultiEdit`
  • **Category**: development

  • Environment Variables


    None required


    Requirements


    None


    Script


    ```bash

    file_path=$(jq -r '.tool_input.file_path // empty')

    if [[ -n "$file_path" ]] && [[ -f "$file_path" ]]; then

    backup_dir=".claude/backups"

    mkdir -p "$backup_dir"

    timestamp=$(date '+%Y%m%d_%H%M%S')

    filename=$(basename "$file_path")

    cp "$file_path" "$backup_dir/${filename}.${timestamp}.bak"

    fi

    ```

    About PreToolUse Hooks

    PreToolUse hooks run before a tool executes. They can validate, modify, or block the tool call.