Chapter 09Lesson 03~95 minutes

find, xargs, and Null-Delimited Workflows

`find` is a filesystem query language. Used with batched `-exec` or NUL-delimited records, it can drive large path-based workflows without ever pretending filenames are ordinary lines of text.

BeginnerFilesystemHands-on lab

Learning objectives

By the end of this lesson

  • Build find expressions with predicates and actions.
  • Use grouping and pruning.
  • Compare -exec terminators.
  • Use NUL-delimited xargs workflows.
  • Preview destructive operations before execution.

1. find is a filesystem query language

find expression model
flowchart LR
  R["root"] --> W["walk tree"]
  W --> P{"predicates"}
  P -->|"match"| A["action"]
  P -->|"no"| W
  A --> W

Predicates select entries and actions operate on matches.

2. Combine predicates to describe the desired set

find ./logs \
  -type f \
  -name '*.log' \
  -mtime +7 \
  -print

3. Group alternatives explicitly

find . -type f \( -name '*.yaml' -o -name '*.yml' \) -print

The parentheses are escaped so the shell passes them to find.

4. -prune skips entire subtrees

find . \
  -path './.git' -prune -o \
  -type f -name '*.sh' -print

5. -exec ... \; runs once per match

find ./configs -type f -name '*.conf' \
  -exec grep -HnF -- 'deprecated_option=' {} \;

6. -exec ... + batches matches

find ./configs -type f -name '*.conf' \
  -exec grep -HnF -- 'deprecated_option=' {} +

Batching reduces process-launch overhead and preserves pathname arguments without converting them to text.

7. -print0 exports safe pathname records

find ./artifacts -type f -print0 |
while IFS= read -r -d '' file; do
  printf 'path=%q\n' "$file"
done

8. xargs turns records into command arguments

find ./artifacts -type f -print0 |
xargs -0 sha256sum --

The -0 option reads NUL-delimited records on GNU and many modern implementations. Check portability requirements.

9. Zero-match behavior is part of the design

Some xargs implementations may invoke the command with no input. GNU -r prevents that, but is not universal.

Portable alternative

When empty-input behavior matters across systems, find -exec ... + is often easier to reason about.

10. xargs can provide bounded parallelism

find ./artifacts -type f -print0 |
xargs -0 -n 1 -P 4 sha256sum --

Parallel workers can change output ordering and resource usage, so concurrency should be an explicit policy.

11. Preview destructive find expressions

find ./cache \
  -type f \
  -name '*.tmp' \
  -mtime +7 \
  -print
Preview first

Verify the match set before replacing -print with a destructive action such as -delete.

12. Hands-on lab: hash arbitrary filenames

mkdir -p "$HOME/devops-academy/bash/chapter09/lesson03/artifacts"
cd "$HOME/devops-academy/bash/chapter09/lesson03"

printf 'a\n' > "artifacts/api build.bin"
printf 'b\n' > $'artifacts/worker\nnightly.bin'
printf 'c\n' > "artifacts/cache.bin"

find artifacts -type f -print0 |
while IFS= read -r -d '' file; do
  sha256sum -- "$file"
done

find artifacts -type f -exec sha256sum -- {} +

Verification checklist

13. Knowledge check

Question 1. What is the difference between -exec ... \; and -exec ... +?

Question 2. Why pair -print0 and xargs -0?

Question 3. What does -prune do?

Question 4. Why preview before -delete?

14. Summary

find combines traversal, predicates, and actions. Use grouping, pruning, batched -exec, or NUL-delimited workflows rather than parsing filenames as ordinary lines.

15. Further reading

  • GNU findutils manual.
  • POSIX find and xargs specifications.
  • GNU findutils security considerations.
  • ShellCheck documentation — find/xargs patterns.
Next lesson

Temporary Files and Directories with mktemp

Continue Chapter 9 by building safer, more predictable filesystem automation.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0 Send only Ethereum/ERC-20 compatible assets to this address.