file-backup — Claude Code hook: what it does, how to install
Saves a timestamped copy of files to a .backups folder before Claude Code modifies them. Useful for quick rollbacks if an edit goes wrong.
npx claude-code-templates@latest --hook development-tools/file-backupWhat file-backup does
This hook listens to the PreToolUse lifecycle event for Edit and MultiEdit tools. Before Claude Code modifies any file, a shell script verifies the $CLAUDE_TOOL_FILE_PATH variable and checks that the target exists.
Once confirmed, the command creates the .backups folder and duplicates the file. The backup receives a timestamped name formatted as YYYYMMDD_HHMMSS.bak. Original contents stay safe before edits happen.
The command relies directly on Unix tools like mkdir, cp, and date. On Windows without Bash or WSL, this shell command fails immediately.
What it helps with
- save a file copy before the agent applies edits
- revert unexpected changes from Edit and MultiEdit
- keep timestamped file versions during refactoring
Example prompts for the agent
- Fix the authentication logic in
src/auth.ts - Rewrite the parsing function in
utils/parser.js - Update deprecated methods in
components/App.tsx
How to install file-backup
1claude-code-templates
npx claude-code-templates@latest --hook development-tools/file-backup2Manually: put the files in place
.claude/settings.jsonThe hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json
Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command
Questions about file-backup
How to install file-backup in Claude Code?
Run npx claude-code-templates@latest --hook development-tools/file-backup in your terminal to append the hook configuration into .claude/settings.json.
What does file-backup do?
It intercepts Edit and MultiEdit tool invocations before execution and copies the targeted file into a .backups directory.
Where are backups saved?
Backups are stored inside the local .backups directory with a timestamp suffix like filename.YYYYMMDD_HHMMSS.bak.
Source text
file-backup.json · MIT
{
"description": "Automatically backup files before editing. Creates timestamped backups in a .backups directory when files are modified.",
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|MultiEdit",
"hooks": [
{
"type": "command",
"command": "if [[ -n \"$CLAUDE_TOOL_FILE_PATH\" && -f \"$CLAUDE_TOOL_FILE_PATH\" ]]; then mkdir -p .backups && cp \"$CLAUDE_TOOL_FILE_PATH\" \".backups/$(basename \"$CLAUDE_TOOL_FILE_PATH\").$(date +%Y%m%d_%H%M%S).bak\"; fi"
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates