Find Exec Batch Processing

Process files in batches using find with exec and a batch size limit to avoid argument list overflow.

BashCli Tooling

Code

bashsnippet
16 lines
#!/usr/bin/env bash
set -euo pipefail

DIR="${1:-.}"
BATCH_SIZE=100

find "${DIR}" -type f -name "*.log" -print0 | \
  xargs -0 -n "${BATCH_SIZE}" -- process_batch

process_batch() {
  echo "Processing $# files"
  for f in "$@"; do
    wc -l "$f"
  done
}
export -f process_batch

How it works

set -euo pipefail puts the script into strict mode: it stops on the first failing command, treats unset variables as errors, and fails a pipeline if any stage fails.

The work is delegated to find, xargs and wc, chained through 1 pipe so each stage’s output feeds the next.

A helper function (process_batch) keeps the repeated logic out of the main flow.

The script leans on parameter expansion 3 times, using forms like ${var:-default} so defaults, trimming, and casing happen inside the shell with no external tool.

The key statements that remain

find "${DIR}" -type f -name "*.log" -print0 | \
  xargs -0 -n "${BATCH_SIZE}" -- process_batch
for f in "$@"; do
  wc -l "$f"

Interactive Tools for This Topic

FAQ

Does the script stop on the first error?
Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
Which external commands does the script rely on?
`find`, `xargs`, `wc` must be available on the PATH environment variable; everything else is shell built-in syntax.