Huny's Dev Blog
huny.dev

Preserve Linux Filenames with find and xargs: NUL Delimiters and Empty Input

Reproduce broken filename parsing in GNU find and xargs, preserve paths with NUL delimiters, skip empty input with -r, and verify checksum output boundaries.

Hun Jang
Hun Jang Dec 5, 2026

When Linux find output is passed to xargs, spaces, newlines, or quotes in filenames can cause paths to split or input to be rejected under the default parsing rules. With GNU tools, use find -print0 and xargs -0 -r to preserve filename boundaries and avoid unnecessary execution on empty input. This article reproduces the problem with four temporary files and verifies NUL separation through checksum output as well.

Conceptual illustration of four differently shaped files preserved within individual boundaries; not an actual processing sequence or measurement.

Written and documentation checked: December 5, 2026. Execution environment: Linux, GNU findutils 4.10.0, GNU coreutils 9.7, CPython 3.12.16. These results are neither a comparison of the latest releases nor a run on another operating system.

A file list is not a line list

The GNU findutils filename-handling documentation explains that filenames may contain spaces and newlines. Default xargs splits arguments on whitespace and interprets quotes. The problem is confusing characters in a filename with parsing syntax. Even without shell command injection, the command may operate on the wrong files.

Quoting the entire input as one string does not restore multiple file boundaries either. The transport format needs an explicit end marker for each file. NUL cannot appear in a pathname, making it suitable for that boundary. Both producer and consumer must use the format.

Element
Problem it solves
Problem it does not solve
find -print0
Records NUL boundaries between paths
A consumer that still reads lines
xargs -0
Splits only on NUL and preserves filenames
Whether to run a command on empty input
xargs -r
Skips command execution on empty input
The target command's option parsing
The target command's --
Separates options from file operands in commands supporting it
A path being replaced after collection

The empty-input policy corresponds to --no-run-if-empty in the GNU xargs options documentation. The test below also checks the installed GNU version's behavior.

Reproduce and fix the problem by inspecting actual arguments

Python only prepares the temporary directory and inspection code. The actual find produces the list and the actual xargs consumes it. No shell=True is used, keeping the test's own shell-quoting issues out of the comparison. Each file contains the single byte x.

The two names containing whitespace are checked first to isolate splitting, followed by a separate check for rejection of a quoted name. The corrected path searches all four files. JSON output distinguishes an actual newline from its two-character escaped representation.

Save the following as filename-demo.py.

Run it without Python's -O option, which disables assert checks.

It exited with code 0 and produced:

The four entries on the first line are the incorrectly split versions of two original paths. The NUL preserved line retains each of the four original paths as one argument. Its \n is a JSON escape; the filename contains an actual newline. Sorting is for comparison, so this does not test or guarantee search order.

On empty input, the reporting command ran once with no filenames under the default policy. With -r, there was no output at all. The final check compares both the four digests and their original paths. A failing assert means this run must not be considered successful.

NUL input alone does not fix output parsing

Even after paths reach the checksum tool correctly, blindly splitting its result on newlines can conflict with newlines inside filenames. The GNU checksum output options provide -z, which terminates result records with NUL and disables filename escaping. The test separates and verifies byte records in that format.

The equivalent shell composition is below. Narrow the search root to the intended task.

Its output is a NUL-separated byte stream, not a human-readable list of lines. Downstream processing must preserve those boundaries too. Paths from find . start with ./, so the test passed -dash.txt as ./-dash.txt. Independently, -- explicitly ends the target command's options. Do not assume every program supports it.

A production script must also check failures from both the find producer and the xargs consumer. This one-liner demonstrates filename transport; it does not implement a complete pipeline error policy. The test code checks the two processes' exit codes separately.

Preserving a name does not guarantee the same file

NUL separation preserves pathname string boundaries. It does not eliminate races where a file is replaced or its contents change between collecting a path and opening it later. Consult the GNU findutils race-condition section and separately design deletion or permission-changing operations in directories other users can modify. This example only reads inside its owned temporary directory; it does not test an attacker racing the operation.

For large inputs, xargs may execute the command multiple times. The JSON reporter tests only the small case where four files fit in one invocation. It does not justify assuming that a general service will receive one invocation or one JSON document.

Filename bytes that cannot be decoded as UTF-8, parallel execution, and tools on other operating systems were not tested. When porting to another implementation, check support for -0, -r, and the checksum tool's -z separately. Safe filename handling requires separate checks for boundaries, option parsing, execution policy, and file ownership.

한국어판: Linux find·xargs에서 파일명이 깨질 때: NUL 구분과 빈 입력 처리Linux find·xargs에서 파일명이 깨질 때: NUL 구분과 빈 입력 처리

추천 글