Use find -exec or xargs to run a command over hundreds of files safely. Learn how to batch arguments, handle odd file names, and run jobs in parallel without mangled output.
The short answer
#Let find pick the files and either run the command itself with -exec ... {} +, or pipe null-separated names into xargs -0. Both pass many files to each command call, so they are fast and do not hit the "Argument list too long" limit.
_10# Run gzip on every .log file under logs/_10find logs -type f -name '*.log' -exec gzip {} +_10_10# The same with xargs, using four jobs at once_10find logs -type f -name '*.log' -print0 | xargs -0 -P 4 gzip
Why it happens
#The obvious approach, gzip logs/*.log or gzip $(find logs -name '*.log'), breaks in two ways as the number of files grows.
First, the kernel limits how long a single command line can be. With enough files, the shell fails with "Argument list too long" before your command even starts. Second, $(find ...) splits its output on spaces and newlines, so a file called March report.log becomes two arguments, neither of which exists.
find -exec and xargs solve both problems. They split the list into batches that fit the limit, and when used correctly they pass each file name as one argument no matter what characters it contains.
How to fix it
#Use find -exec with a plus sign
Ending -exec with {} + tells find to collect as many names as fit and run the command once per batch. Ending it with {} \; runs the command once per file, which is much slower for large sets.
_10# One call per batch: fast_10find src -name '*.ts' -exec prettier --write {} +_10_10# One call per file: needed when the command takes only one file_10find images -name '*.png' -exec optipng -o2 {} \;
Because find hands the names straight to the command without a shell in between, spaces, quotes and newlines in file names are safe.
If the command needs to run inside each file's folder, use -execdir in the same way.
Use xargs -0 when you want more control
xargs reads names from standard input and builds command lines from them. Always pair find -print0 with xargs -0 so names are separated by a null byte, the one character a file name cannot contain.
_10find . -name '*.tmp' -print0 | xargs -0 rm --
Useful options:
-n 10passes at most ten files per call.-I {}puts each name wherever{}appears, running one command per file.-r(GNU) skips running the command when there is no input. BSD and macOSxargsalready behave this way and accept-rfor compatibility.
_10find photos -name '*.heic' -print0 |_10 xargs -0 -I {} magick {} {}.jpg
Run jobs in parallel with -P
xargs -P starts several commands at once. Combine it with -n so each job gets a sensible share of the work.
_10# Up to 8 jobs, 20 files each_10find videos -name '*.mov' -print0 |_10 xargs -0 -n 20 -P 8 ./transcode.sh
Pick a job count near the number of CPU cores (nproc on Linux, sysctl -n hw.ncpu on macOS) for CPU-heavy work. For network-bound work you can go higher, but be kind to the server you are calling.
Use a while read loop for multi-step work
When each file needs several commands or some logic, a loop is easier to read than a long xargs line.
_10find data -name '*.csv' -print0 |_10 while IFS= read -r -d '' file; do_10 rows=$(wc -l < "$file")_10 echo "$file: $rows rows"_10 done
This runs one file at a time, which is fine for most scripts.
Common pitfalls
#- Using
xargswithout-0****. Plainxargssplits on spaces and treats quotes specially, so a name with an apostrophe makes it stop with an error. Use-print0and-0together every time. - Interleaved output in parallel runs. With
-P, output from different jobs can mix line by line. Write each job's output to its own file, or use GNU Parallel, which keeps each job's output together. - Jobs that write to the same file. Two parallel jobs appending to one log or editing one file can corrupt it. Make sure each job only touches its own outputs.
- Forgetting
--****. A file called-rflooks like an option torm. Put--before the file names, or use./paths, so commands treat them as names. - Testing on the real files first. Put
echoin front of the command, for examplexargs -0 echo rm --, to see exactly what will run before you run it. - Exit codes.
find -exec ... +andxargsreport failure if any command failed, but-exec ... \;does not affectfind's exit status. Check the output if you rely on errors stopping a script.
Related reading
#Was this resource helpful?