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.
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 | sortThe 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.
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.
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.