Skip to main content
Bash and Linux

Run a Command on Found Files

mediumfind and xargs

Problem statement

Find every Kubernetes manifest that uses an image with the :latest tag, including a file whose name has a space, by passing the results of find to grep safely. :latest makes deploys unpredictable, because the same manifest can pull a different image tomorrow, so many teams block it in review. The bigger lesson is how to run any command on files found by find.

The script creates these manifests:

k8s/

TEXT
web.yaml image: nginx:latest
api.yaml image: myapp:1.4.2
batch.yaml image: busybox:latest
my worker.yaml image: redis (no tag at all)
  1. Show what plain xargs does with the file name that has a space.
  2. List the files that use :latest, safely, with -print0 and xargs -0.
  3. Do the same with find -exec ... {} +.
  4. List the files whose image has no tag at all, which also means latest.

Expected output:

TEXT
== plain xargs: grep's errors, because the name with a space was split in two ==
grep: ./k8s/my: No such file or directory
grep: worker.yaml: No such file or directory
== safe: -print0 and xargs -0 ==
./k8s/batch.yaml
./k8s/web.yaml
== safe: find -exec ... {} + ==
./k8s/batch.yaml
./k8s/web.yaml
== images with no tag at all (also means latest) ==
./k8s/my worker.yaml

Hints

Hint 1: Plain xargs splits its input at spaces, so my worker.yaml becomes two names. find -print0 ends each name with a null character and xargs -0 splits only there.

Approach

Optimal: find -print0 with xargs -0

Covers: xargs, why plain xargs breaks on spaces, find -print0, xargs -0, xargs -r, -exec {} + vs -exec {} \;, grep -l.

Turning a list into arguments. find prints a list of paths. Many commands, like grep, want paths as arguments, not as input lines. xargs reads lines from its input and adds them as arguments to a command: find ... | xargs grep -l latest becomes grep -l latest ./k8s/web.yaml ./k8s/api.yaml ....

Where plain xargs breaks. By default, xargs splits its input at spaces and newlines. ./k8s/my worker.yaml becomes two arguments, ./k8s/my and worker.yaml, and grep complains that neither file exists. Quotes and backslashes in names cause similar trouble.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart LR subgraph BAD["find | xargs grep"] direction TB B1["./k8s/my worker.yaml"]:::red --> B2["./k8s/my"]:::red B1 --> B3["worker.yaml"]:::red end subgraph GOOD["-print0 | xargs -0"] direction TB G1["./k8s/my worker.yaml"]:::green --> G2["one name, intact"]:::green end BAD ~~~ GOOD classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style BAD fill:transparent,stroke:#dc2626,stroke-width:2px style GOOD fill:transparent,stroke:#059669,stroke-width:2px

The null character fix. File names on Linux can contain almost anything, including spaces and newlines, but never the null character. So:

Piece Does
find ... -print0 ends each path with a null character instead of a newline
xargs -0 splits its input only at null characters
xargs -r run nothing when the input is empty (GNU)

Together, every name arrives in one piece, whatever it contains.

-exec does it without xargs. find can run the command itself:

Form Runs
-exec grep -l latest {} + one grep with many files at once (fast)
-exec grep -l latest {} \; one grep per file (slow, but simple)

{} is where find puts the path or paths, and + or \; ends the command. Both forms are safe with spaces, because find passes each name directly, without splitting.

Walking through the code. The # Setup: lines only create the four manifests, so skip past them.

  1. Plain xargs gets the name in two pieces. 2>&1 > /dev/null keeps only grep's error messages (remember the order rule from the redirects page), which show the two broken names.
  2. -print0 | xargs -0 lists both :latest files correctly.
  3. -exec ... {} + gives the same list.
  4. grep -E 'image: [^:]+$' finds an image: line where the name has no : up to the end of the line, which is the untagged redis.
%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB I1["image: nginx:latest
flagged"]:::red ~~~ I2["image: redis
no tag, flagged"]:::red I2 ~~~ I3["image: myapp:1.4.2
pinned, fine"]:::green classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px

Edge cases. An image with a registry port, like registry:5000/app, contains a : that is not a tag; check it by eye or use a YAML tool. Digest-pinned images like app@sha256:... are the safest form of all, and the patterns here correctly do not flag them as :latest.

# Setup: create the sample manifests (one name has a space) in a fresh temporary folder
cd "$(mktemp -d)"
mkdir k8s
printf 'kind: Deployment\nimage: nginx:latest\n'   > k8s/web.yaml
printf 'kind: Deployment\nimage: myapp:1.4.2\n'    > k8s/api.yaml
printf 'kind: Job\nimage: busybox:latest\n'        > k8s/batch.yaml
printf 'kind: Deployment\nimage: redis\n'          > "k8s/my worker.yaml"

echo "== plain xargs: grep's errors, because the name with a space was split in two =="
find . -name '*.yaml' | xargs grep -l ':latest' 2>&1 > /dev/null | sort

echo "== safe: -print0 and xargs -0 =="
find . -name '*.yaml' -print0 | xargs -0 grep -l ':latest' | sort

echo "== safe: find -exec ... {} + =="
find . -name '*.yaml' -exec grep -l ':latest' {} + | sort

echo "== images with no tag at all (also means latest) =="
find . -name '*.yaml' -exec grep -lE 'image: [^:]+$' {} + | sort

Interview follow-ups

  • Rewrite every :latest tag to a pinned version, safely.

    Find the files, then edit them in place with a backup: find . -name '*.yaml' -exec sed -i.bak -E 's/(image: [^:]+):latest$/\1:1.25.3/' {} +. The capture group keeps the image name and swaps only the tag. Review the change with git diff before committing, and delete the .bak files after. In practice each image needs its own version, so a small map of image to version, read in a loop, is the real-world answer.

Frequently asked questions

latest is just a name that points at whatever image was pushed last. Two nodes can pull different images for the same manifest, a rollback can pull the broken version again, and you cannot tell from the manifest what is running. Pinning a version tag like 1.4.2, or better a digest like @sha256:..., makes every deploy repeatable. Kubernetes also defaults imagePullPolicy to Always for :latest, which adds a registry call to every pod start.

Use -exec {} + for most jobs: it is safe with any file name and needs no extra tool. Use xargs -0 when you want features xargs has, like running jobs in parallel with -P 4 or limiting files per command with -n 50. Avoid -exec {} \; on big trees, because it starts one process per file, which is much slower. Never use plain xargs on file names you do not control.

GNU xargs still runs the command once, with no file arguments. For grep, that means it reads from your terminal and seems to hang, or exits with an error. Add -r (--no-run-if-empty) so nothing runs when find found nothing. find -exec does not have this problem, because it only runs when there is at least one match. BSD xargs on macOS already behaves like -r.