All articles

Find Files Safely with -print0, xargs -0, and -exec

6 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Find Files Safely with -print0, xargs -0, and -exec

Filenames may contain spaces, quotes, tabs, and newlines. A pipeline that treats newline as a filename separator will eventually split one pathname into several arguments.

GNU find -print0 writes a NUL byte after each pathname, and xargs -0 reads that exact format 1. POSIX.1-2024 now specifies both forms, although older systems may still lack them 3.

Inspect the match set first

Begin with a command that only prints:

find ./logs -xdev -type f -name '*.log' -print

-xdev keeps the walk on one filesystem. It can prevent a search from entering mounted backups, network shares, or container filesystems below the starting directory.

Group alternatives explicitly:

find ./work -type f \( -name '*.log' -o -name '*.tmp' \) -print

Without parentheses, -a and -o precedence can apply actions to only part of the expression. POSIX defines expression evaluation and the implicit logical AND between adjacent primaries 3.

Preserve arbitrary names

This is unsafe when filenames are data:

find ./work -type f -print | xargs command

Whitespace and quotes are syntax to ordinary xargs, so its arguments may not match the names produced by find. The NUL-delimited form removes that ambiguity 2:

find ./work -type f -print0 |
  xargs -0 -r command

GNU xargs -r skips command when no path matches. That option is not portable to every implementation. find -exec avoids the empty-input question:

find ./work -type f -exec command -- {} +

{} + batches multiple pathnames per invocation. Use {} \; only when the command must run once per file.

A filename lab

The lab creates ordinary, spaced, and newline-containing names:

lab=$(mktemp -d)
newline_name=$(printf 'line\nbreak')

: > "$lab/plain"
: > "$lab/name with spaces"
: > "$lab/$newline_name"

find "$lab" -type f -print0 |
  xargs -0 -r -n 1 printf 'argument=[%s]\n'

The third pathname occupies two display lines because the newline belongs to the name. It still reaches printf as one argument.

For a machine-readable inventory, keep the NUL delimiter in the output:

find "$lab" -type f -printf '%p\0' > "$lab/files.list"

Do not open a NUL-delimited file in tools that assume lines. Pass it to a NUL-aware consumer.

Prune directories deliberately

To skip .git while walking everything else:

find . -path './.git' -prune -o -type f -print

The left branch matches .git and stops descent there. The right branch handles the remaining paths. Add parentheses when the expression grows; compressed one-liners are hard to review.

Keep mutation separate

Caution: -delete, -exec with a mutating command, and xargs can change every matched path. Save and review the exact match set first. Re-run the predicate immediately before mutation because the tree may have changed.

For shared writable trees, find -execdir can reduce some pathname races by running the command from the matched entry’s subdirectory. Its security still depends on a trustworthy PATH and the called program 2.

A safe review records the starting directory, filesystem boundary, complete predicate, delimiter, empty-input behavior, and final command. Each part controls which pathnames become arguments.

Sources and further reading
  1. GNU Findutils - Safe File Name Handling
  2. GNU Findutils - Security Considerations for xargs
  3. POSIX.1-2024 find