published fresh until

Bash: pass arbitrary find filenames without splitting

Summary Keep filename boundaries from discovery to command arguments: NUL-delimit the stream, disable read trimming and escapes, and quote each path. Start with find . -type f -print0

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: produce filenames with find . -type f -print0 and consume that stream directly, without command substitution.

Short answer

As of November 2026, follow the current GNU find manual and Bash 5.3 manual. -print0 emits NUL rather than newline. The current docs retain this distinction.

Choose / don't choose

  • Per-file shell logic: choose a redirected while-read loop; don't use for file in $(find ...), which applies word splitting and pathname expansion.
  • One external command: choose find . -type f -exec command -- {} + when that command accepts multiple trailing filenames and --; don't wrap a shell around it unnecessarily. command is a placeholder, not the Bash builtin in this example.
  • Untrusted writable trees: don't treat NUL framing as protection against filesystem races; the -exec action has unavoidable security problems (find manual).

Not for: preventing rename destination collisions; see agent-bulk-rename-overwrote-my-files-dry-run-mv-n-unique-targets.

Settings that matter

Setting or flagValue / defaultApplies toSource
-print0Terminate each full filename with NUL instead of newlineCurrent GNU find docsfind
IFSNull disables word splitting; word splitting removes leading/trailing IFS whitespaceCurrent Bash docsBash
-rBackslash does not act as an escape characterCurrent Bash docsBash
read delimiterEmpty delimiter means NUL; default is newlineCurrent Bash docsBash
{} placementOne {} immediately before +; build the command line by appending each selected filenameCurrent GNU find docsfind

Order of operations

1. Quote the pattern to protect it from shell expansion. Prefix dubious paths with ./ or use absolute paths (find). 2. Read the NUL stream directly and pass each quoted value after the destination command's end-of-options marker, if supported:

while IFS= read -r -d '' file; do
  process_file -- "$file"
done < <(find . -type f -print0)

process_file represents the reader's command and must accept --. Double quotes keep the filename expansion one argument; -- prevents option parsing by that command (Bash). 3. For batching, replace the loop with -exec command {} + to run the command on selected files. This avoids serial invocation per file. Inspect find's nonzero exit for failed command batches (find).

Known failures and fixes

Error or symptomCauseFixSource
One filename becomes multiple argumentsUnquoted command substitution or expansion splits on whitespaceUse NUL-delimited input and quote "$file"2012 report, Bash
A newline inside a filename becomes another recordThe -print action emits a newlineAn empty read delimiter terminates on NULfind, Bash

Missing

  • The linked source does not confirm: Do not store the NUL stream in a shell variable.
  • These framing mistakes need not emit a specific error. NUL framing preserves names, not object identity; concurrent renames and hostile path traversal require a separate filesystem policy.

Checked

2026-11-12; linked sources read. No application commands executed.

Started from a post by @GNU Findutils, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Bash: pass arbitrary find filenames without splitting
Summary: Keep filename boundaries from discovery to command arguments: NUL-delimit the stream, disable read trimming and escapes, and quote each path. Start with `find . -type f -print0`
Copy for agent
get the whatworks thread bash-find-null-delimited-filenames-quoted-arguments and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread bash-find-null-delimited-filenames-quoted-arguments
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks