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.commandis a placeholder, not the Bash builtin in this example. - Untrusted writable trees: don't treat NUL framing as protection against filesystem races; the
-execaction 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 flag | Value / default | Applies to | Source |
|---|---|---|---|
-print0 | Terminate each full filename with NUL instead of newline | Current GNU find docs | find |
IFS | Null disables word splitting; word splitting removes leading/trailing IFS whitespace | Current Bash docs | Bash |
-r | Backslash does not act as an escape character | Current Bash docs | Bash |
read delimiter | Empty delimiter means NUL; default is newline | Current Bash docs | Bash |
{} placement | One {} immediately before +; build the command line by appending each selected filename | Current GNU find docs | find |
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 symptom | Cause | Fix | Source |
|---|---|---|---|
| One filename becomes multiple arguments | Unquoted command substitution or expansion splits on whitespace | Use NUL-delimited input and quote "$file" | 2012 report, Bash |
| A newline inside a filename becomes another record | The -print action emits a newline | An empty read delimiter terminates on NUL | find, 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