OmniscioGuidesA hook that blocks rm -rf
A Claude Code hook that blocks rm -rf and its neighbours
An agent with a shell is useful because it can run anything, and dangerous for the same reason. One rm -rf in the wrong directory is not something a follow-up turn can undo. The hook below reads the Bash and PowerShell commands Claude is about to run — except the commands a skill injects while it loads — and blocks the ones matching its pattern list before they start.
Checked against Anthropic’s Claude Code hooks documentation on November 25, 2026. By the Omniscio team. Updated .
The problem
Permission prompts catch a command the first time and then stop asking once you approve a shape. A rule that must hold every time, in every mode, is not a prompt; it is a check that runs before the tool does. Anthropic’s hooks reference describes PreToolUse as running after Claude builds the tool’s parameters and before the tool call is processed, which is exactly the window a policy needs.
The documentation ships a small example of this: a script that blocks rm commands by exiting 2. The version below extends it to a list of patterns, and it runs in Git Bash on Windows as well as on macOS and Linux.
The script
Save it as .claude/hooks/block-destructive.sh in the project, create the folder if it does not exist, and make the file executable. It needs jq on PATH: brew install jq on macOS, sudo apt-get install jq on Debian and Ubuntu, winget install jqlang.jq on Windows.
#!/bin/bash
# .claude/hooks/block-destructive.sh
input=$(cat)
command -v jq >/dev/null || { echo "Blocked: block-destructive.sh needs jq on PATH." >&2; exit 2; }
cmd=$(jq -r '.tool_input.command // empty' <<<"$input")
[ -z "$cmd" ] && exit 0
BLOCKED=("rm -r" "rm -fr" "rm -f -r" "rm --recursive" "rm --force" \
"git push -f" "git push --force" "git reset --hard" \
"git clean -" "chmod -R 777")
shopt -s nocasematch
for pattern in "${BLOCKED[@]}"; do
if [[ "$cmd" == *"$pattern"* ]]; then
echo "Blocked: this command contains '$pattern'. Ask the user before running a destructive command." >&2
exit 2
fi
done
exit 0Everything the script decides is in its exit codes. 2 stops the call and sends the one-line message back to Claude. 0 means no decision, so the call continues into the normal permission flow, exactly as Anthropic’s own example describes it.
Each entry is a substring, not a glob. rm -r covers rm -rf and rm -r -f; rm -fr and rm -f -r are listed separately because neither contains rm -r. Two honest consequences: a spacing variant such as rm -rf slips through, and git push --force also catches git push --force-with-lease, the safer form. Delete or narrow a pattern if it blocks something you rely on.
Registering it
Merge this into .claude/settings.json at the project root rather than replacing the file: if it already has a "hooks" object, add the PreToolUse array inside it. The matcher names both shell tools, which matters on Windows: Anthropic documents that a hook matching only Bash never fires where the PowerShell tool is enabled.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|PowerShell",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-destructive.sh",
"timeout": 15,
"onFailure": "block"
}
]
}
]
}
}The script is Bash, so on Windows it runs in Git Bash and the patterns above are Unix ones: a PowerShell command such as Remove-Item -Recurse -Force will not match them. Either install Git for Windows and keep this script, or add a second handler with "shell": "powershell" and its own patterns. If you skip both, a Windows reader gets a hook that either cannot start or matches nothing.
The onFailure line is what turns this from a suggestion into a policy. Without it, a hook that cannot start — a typo in the path, a file that lost its execute bit — is a non-blocking error on most events, so every command goes through. With it, a failure or a timeout blocks the call instead. A missing jq is a different case, which is why the script tests for it and exits 2 on its own: the script would otherwise start, read nothing, and exit 0. Anthropic notes the field requires Claude Code v2.1.295 or later, so run claude --version first: on an older build the field is ignored and a broken guard fails open.
What each line does
| Line | Job |
|---|---|
input=$(cat) | Reads the event JSON that Claude Code writes to the hook’s stdin. |
command -v jq || … exit 2 | Fails closed if jq is missing: the message goes to stderr and the call is blocked, rather than read as an empty command and allowed. |
jq -r '.tool_input.command' | The command string for a Bash or PowerShell call. // empty keeps a missing field from printing null. |
[ -z "$cmd" ] && exit 0 | No command means nothing to judge, so the hook declines to decide. This line is also why a missing jq fails open: no output means no command to check. |
BLOCKED=(...) | The patterns, compared as substrings anywhere in the command. |
shopt -s nocasematch | Makes the comparison case-insensitive, so rm -R on macOS is caught too. |
for pattern in ... | Checks each pattern and exits on the first hit; there is no loop over stdin, so nothing can swallow the exit code. |
echo ... >&2 | The message goes to stderr, because that is what Claude Code reads as the blocking reason. |
exit 2 | The blocking error. On PreToolUse it prevents the tool call. |
exit 0 | No decision. The call continues through the ordinary permission flow. |
What Claude sees is the script’s stderr, prefixed so the source is obvious. Anthropic’s example shows the line as PreToolUse:Bash hook error: [${CLAUDE_PROJECT_DIR}/.claude/hooks/no-rm.sh]: Blocked: rm commands are not allowed.
Why exit 2 and never exit 1
Every other tool you own treats a non-zero exit as failure. Claude Code does not.
It reads three outcomes: 0 is success, 2 is a blocking error on the events that can block, and any other code is a non-blocking error — the action goes ahead and you see a <hook name> hook error notice in the transcript.
The reference states the trap directly: exit code 1 without valid JSON output is a non-blocking error, even though 1 is the conventional Unix failure code, and a hook meant to enforce a policy should use exit 2. One qualification matters here, because the block above sets onFailure: "block": an exit code other than 0 or 2 then counts as a hook failure, and a failure blocks. So a script that exits 1 on a refusal is blocked in this configuration — but it reads as a broken hook rather than as your Blocked: message, and on the events where onFailure has no effect it blocks nothing. Use exit 2 for a deliberate block.
The same rule applies to a crash. A script that dies on an unset variable exits with a code the shell picks, and that code is not 2, so the command runs. This is the second reason for onFailure: "block": it makes a failed or timed-out hook block the call, so a broken guard fails closed instead of open.
What it does and does not catch
These are the edges, and a reader should know them before trusting the hook.
- It judges the text of the command, not its effect.
bash clean.sh,npm run clean,make clean,find . -deleteandpython -c "shutil.rmtree('build')"all pass, because the destructive work happens inside another program. A command matcher cannot see there. - It is a guardrail, not a sandbox. Anthropic’s guidance is that a hard allow or deny belongs in the permission system, not a hook; the permission modes guide covers those rules. Keep the hook for the case permission rules handle badly: a policy that must survive every mode, including bypass permissions.
- It only sees commands Claude runs through a shell tool. Anthropic warns that
PreToolUsedoes not fire for the shell commands a skill runs while it loads, and givesPreSkillShellCommand, a separate hook event for exactly those. A hook with aBashmatcher also runs for Monitor commands, where its allow and rewrite have no effect, though a deny still counts. - A blocked call is not a repaired one. Claude receives your message as the tool error and can try something else; it is not told to stop working. Write the message as an instruction: ask the user before running a destructive command is more useful than nope.
The permission-mode rule is the reason this is worth writing: a hook’s block holds in every permission mode, including bypass permissions, because PreToolUse fires before the mode check. That is what makes it different from a prompt.
How to test that it fires
- Run the script on sample JSON and read the exit code. From the project root, each line prints the code the hook would return:
echo '{"tool_name":"Bash","tool_input":{"command":"rm -rf /tmp/build"}}' | .claude/hooks/block-destructive.sh; echo $? echo '{"tool_name":"Bash","tool_input":{"command":"rm -fr /tmp/build"}}' | .claude/hooks/block-destructive.sh; echo $? echo '{"tool_name":"Bash","tool_input":{"command":"cd /tmp && rm -rf build"}}' | .claude/hooks/block-destructive.sh; echo $? echo '{"tool_name":"Bash","tool_input":{"command":"ls -la"}}' | .claude/hooks/block-destructive.sh; echo $?The first three must print2with theBlocked:line, and the last must print0. A0on a command that plainly contains a pattern is usually a missingjqor invalid JSON, not a bad pattern list: checkecho '{"tool_input":{"command":"rm -rf x"}}' | jq .tool_input.commandfirst. - Check the registration.
/hookslists the hook underPreToolUse; selecting it shows the settings file it came from. - Ask Claude to run it. Ask for
rm -rf /tmp/some-folderand watch the transcript: the tool call is refused and the message you wrote is the error. If the call runs anyway, either the hook never executed — check the path, the execute bit and the matcher — or it ran and failed open, which step 1 tells apart.
The common failures
1. The hook never runs
The usual cause is the path. A settings file is not a shell, so ~ and relative paths are a gamble; $CLAUDE_PROJECT_DIR is the documented way to name a script inside the project, quoted in shell form. A script without the execute bit fails with the interpreter’s message instead of running. Without onFailure set, none of this blocks anything on macOS, Linux, or Windows with Git Bash; on Windows without Git Bash a failing shell-form hook can block by itself, which the next failure covers.
2. The message says the tool was blocked, but the command ran
That means the hook blocked something else, or the call you watched was not the one it matched. Matchers are case sensitive and match tool names exactly, so bash matches nothing. Anthropic’s hook-not-firing checklist includes this check; it starts with /hooks and lists the matcher check after that.
3. Windows behaves differently
Without Git Bash, a shell-form PreToolUse hook that fails or times out can block the call by itself, and Anthropic documents the exact message: blocking because Git Bash wasn't found, so only exit code 0 or 2 counts as this hook's verdict. That message means the hook failed or timed out — on Windows without Git Bash any exit code other than 0 or 2 counts as a failure — so it is not a verdict that your command was dangerous.
4. It blocks too much
A pattern that is too broad turns into a hook people delete. Start with the commands you have actually seen go wrong, keep the list short enough to read, and put the explanatory sentence in the message so the block teaches instead of confusing.
Where Omniscio fits
This is a project-level guard, so commit .claude/settings.json and the script with it and every clone inherits the rule. Omniscio’s Hooks panel takes the session-level angle instead: it composes the hooks it ships, and any you define, into every session it spawns, with a global switch and per-project overrides, kept in its own database rather than written into your global Claude configuration. The Claude Code hooks guide covers the panel, and the hooks builder assembles the block above from the event, matcher and handler you pick.
Omniscio runs on Windows, macOS and Linux, and the pricing page covers the plans.
How this page was written
The event, the exit-code contract, the onFailure field, the matcher rule and the Windows behaviour were read from Anthropic’s Claude Code hooks reference on November 25, 2026, quoted from the page as it reads today. The script shape is Anthropic’s example, extended with a pattern list and the Bash details that come with it; the page says which parts are ours.
No Claude Code session was configured to run this hook for the page, so the exit codes above are the ones the script is written to return and the four test lines are there for you to run. The limits section says what the script cannot see, because the documentation and the script’s own logic both show it.
Sources
- Anthropic’s Hooks reference, for
PreToolUse, its input, the exit-code table, the exit-1 warning,onFailure, the matcher andifrules, the Windows notes and the security guidance, read November 25, 2026. - Anthropic’s Automate actions with hooks, for the hook-not-firing checklist, the manual pipe test and the permission-mode limits, read November 25, 2026.
Common questions
How do I block a dangerous command in Claude Code?
Write a PreToolUse hook with a Bash matcher. It receives the command on stdin as tool_input.command, and a hook that exits 2 blocks the tool call before it runs. Exit 0 lets the call go through the normal permission flow.
Why does my hook exit 1 and still not block anything?
Because 1 is not the blocking code. Anthropic documents exit 2 as the blocking error and says that exit 1 without valid JSON output is a non-blocking error, even though 1 is the conventional Unix failure code. With onFailure unset the command runs; with onFailure: "block" it is blocked as a failed hook, without your message. Use exit 2 for a deliberate refusal.
Is a hook a real security boundary?
Not on its own. Anthropic’s guidance is that a hard allow or deny belongs in the permission system, and a command-matching script cannot see what another program will do. Deny rules are the hard stop; the hook is the guard that also works in bypass permissions mode.
Does the hook still work in bypass permissions mode?
Yes. Anthropic documents that PreToolUse hooks fire before any permission-mode check, in every mode, so a hook that denies a call holds even under bypass permissions or --dangerously-skip-permissions.
What if the script itself crashes?
With onFailure unset, a crash counts as a non-blocking error and the command runs, which is the wrong default for a guard. Set onFailure: "block" so a failed or timed-out hook blocks the call, and check claude --version first, because the field requires v2.1.295 or later.
A guard that holds in every session
Omniscio runs your Claude Code sessions side by side and composes the hooks it ships, and any you define, into every one it spawns, so a rule you wrote once is not missing from the session you forgot to configure.
The features page covers what else it does, pricing covers the plans, and you can download it for Windows, macOS or Linux.