$_ CLI In Depth
Learning progress 0%
lesson files globbing find

find -exec, xargs & Null-Delimited Pipelines

6 min read Module 4: File Manipulation, Globbing & find Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

The problem with newlines

File names can contain any byte except the slash and NUL, including spaces, tabs, and newlines. A pipeline that passes names on separate lines breaks when a name contains a newline, because the receiver splits it in two. A pipeline that passes names separated by spaces breaks on any name with a space. The solution is to use the NUL byte as the separator, because it cannot appear in a name. The find action -print0 and the xargs option -0 work together for this purpose.

BASH5 lines
cd /workspace
mkdir -p batch
touch batch/one.log batch/two.log batch/three.txt
touch "batch/with space.txt"
find batch -type f -print0 | xargs -0 -n 1 basename | sort

The output lists the four base names in sorted order, including the one with a space, which a newline-based pipeline would have broken into two words.

find -exec with a semicolon

The action -exec command {} \; runs the command once for each match, with the match substituted for the two braces. The escaped semicolon ends the command. This form is simple and always correct, because each name is passed as one argument. Its cost is that it starts one process per file, which is slow for thousands of files.

The braces are replaced by the name as a single argument, even if the name contains spaces, so the expansion is safe. The command runs in the directory where find was started, and find prints nothing on its own, because the action has replaced the implicit print.

find -exec with a plus sign

The form -exec command {} + collects many names and passes them to the command in batches, the same way xargs does. It is much faster for large sets, and it is also safe with spaces, because the names are passed as separate arguments and not as a string. The batches may be split into several invocations, so a command that expects all the names at once must not rely on a single call.

BASH3 lines
cd /workspace
find batch -name '*.log' -exec cat {} +
find batch -name '*.log' -exec echo would remove {} +

The first command concatenates the log files. The second prints the names as a dry run before you delete anything, which is a good habit for any action that changes files.

xargs as a separate tool

The program xargs reads items from standard input, splits them according to the options, and runs the command you give with those items appended. The option -0 tells it to split on NUL, which matches the output of find -print0. The option -n limits the number of arguments per call, and -I substitutes each item at a chosen place in the command. The option -P runs several commands in parallel, which can speed up independent work such as compressing many files.

Xargs is useful when the list comes from a program other than find, such as a list produced by grep or sort. The rule is the same: produce the list with NUL separators when the names may be unusual, and consume it with -0.

Warning

Without -0, xargs splits on whitespace and treats quotes and backslashes specially, which corrupts names. It is the most common source of files that were not found by a pipeline that appeared to work. Always pair -print0 with -0.

Empty input

If the input list is empty, xargs still runs the command once with no arguments by default. This can cause a surprise: a command such as rm would run with nothing, which is harmless, but some commands print a usage message or behave differently. The GNU option --no-run-if-empty, written as -r, stops the command from running when there is nothing to process.

Choosing the right tool

Use find with -exec and the plus sign when the action is a single command. Use find -print0 with xargs -0 when the action is a pipeline or when several stages must receive the same list. Use a loop with read -d with the NUL delimiter when the action needs shell logic. The ordering and safety rules are the same in all three cases.

Choosing between the batch forms

The batch form of -exec and xargs both pass names in groups, and they differ in one way that matters. The -exec form runs the command in the directory where find started, and it never reads standard input. The xargs form reads names from standard input, so the command inherits standard input from the pipeline and can be surprised by it. Commands that read from standard input when no file is given, such as cat without arguments, will wait for input if xargs passes none. The -r option avoids that case.

For a single action that must run in a specific directory, the simplest form is a subshell, as in (cd dir && find . -name pattern -exec command {} +). It keeps the path logic in one place and avoids passing absolute paths through every stage.

Error handling in batch actions

A batch action can fail on some of its names and succeed on others. With -exec and the semicolon form, each file gets its own status, and find itself returns nonzero when any command fails. With the plus form, a failing batch stops that batch, and the remaining batches still run. Check the exit status of the whole pipeline, and log the names that failed, so you can retry them.

Be careful with a pipeline that ends in xargs. The status of the pipeline is the status of the last command unless you set pipefail. Turn on pipefail in scripts that must notice failures in an early stage, and verify that the output of the batch is what you intended before you assume it succeeded.

Practice in the lab

The lab concatenates the log files with find -exec, lists base names through a NUL-separated pipeline, and prints a dry run of a removal. Each output is saved and checked in sorted form, so the checker does not depend on the order in which find visits the files.

find: walk, test left to right, then act

Visualizer

find walks a tree and checks each entry against a list of tests, left to right, stopping at the first false one. The action decides what happens to entries that pass.

1 / 6
  • .
  • src
  • main.c
  • util.h
  • tests
  • t1.c
test, left to rightresult
-name '*.c'no
-type fnot checked
-printnothing printed
$ find . -name '*.c' -type f

find starts at . and tests that entry first. The name test fails, so -type f is skipped. -and is implied between tests, and evaluation stops at the first false one.

cd "$(mktemp -d /tmp/find.XXXX)" && mkdir -p src tests && touch src/main.c src/util.h tests/t1.c && find . -name "*.c" -type f
Terminal lab · 3 steps · real Linux in your browser

Batch actions with find and xargs

Concatenate files with -exec, list base names through a NUL pipeline, and dry-run a removal.

01
Concatenate with -exec
Concatenate every .log file under batch into logs-combined.txt, using -exec with a plus sign: ``bash find batch -name '*.log' -exec cat {} + > logs-combined.txt ``
Show hint for step 1
find may visit the files in any order, so the check sorts the lines.
02
List base names through a NUL pipeline
Print the base name of every regular file under batch, separated by NUL bytes, and sort the names into names.txt: ``bash find batch -type f -print0 | xargs -0 -n 1 basename | sort > names.txt ``
Show hint for step 2
xargs -0 reads NUL-separated input, so the name with a space stays whole.
03
Dry-run a removal
Show what removing the .log files would do, without removing anything. Save the output: ``bash find batch -name '*.log' -exec echo would remove {} + > preview.txt ``
Show hint for step 3
Replacing rm with echo is the safe way to preview a batch action.
Check yourself Question 1 of 4
Concept Check NUL-separated output recall

Which find action writes each name followed by a NUL byte instead of a newline?

Recap & Key Takeaways

NUL is the only byte that cannot appear in a file name, so -print0 with xargs -0 is the safe way to pass names between programs.
find -exec with a plus sign batches names as separate arguments and is much faster than one process per file.
xargs -r stops the command from running when its input is empty.
Run a dry run with echo before any action that deletes or changes files.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue