Bash and Linux

Find manifests using an image's :latest tag

mediumfind and xargs

Problem statement

Your team bans the :latest image tag in Kubernetes manifests. List every .yaml or .yml file under k8s/ that contains an image: line ending in :latest, sorted. One file name contains a space, and your command must handle it. Files in the tree:

k8s/api deployment.yaml

YAML
spec:
containers:
- name: api
image: nginx:latest

k8s/web.yaml

YAML
spec:
containers:
- name: web
image: myorg/web:1.4.2

k8s/worker.yml

YAML
spec:
containers:
- name: worker
image: myorg/worker:latest

k8s/jobs/backup.yaml

YAML
spec:
containers:
- name: backup
image: postgres:16

k8s/jobs/cleanup.yaml

YAML
spec:
containers:
- name: cleanup
image: busybox:latest

Examples

Example 1

Input: Run the command from the directory that contains `k8s/`.

Output: k8s/api deployment.yaml k8s/jobs/cleanup.yaml k8s/worker.yml

Hints

Approach

Optimal

find k8s -type f lists regular files. \( -name '*.yaml' -o -name '*.yml' \) is an OR of two name tests; the parentheses are escaped so the shell passes them to find, and without them -o would bind wrongly with -type f. -print0 ends each path with a NUL byte instead of a newline. xargs -0 splits its input on NUL, so api deployment.yaml arrives as one argument, and it packs many files into each grep call. grep -l 'image:.*:latest$' prints the names of files with an image line ending in :latest, and sort makes the order stable.

Bash
find k8s -type f \( -name '*.yaml' -o -name '*.yml' \) -print0 | xargs -0 grep -l 'image:.*:latest$' | sort

Follow-up questions

  • How would you also catch images that have no tag at all (which also means latest)?
  • How would you enforce this in CI so the pipeline fails when a match is found?

Frequently asked questions

Without -print0/-0, xargs splits on whitespace, so k8s/api deployment.yaml becomes two arguments, k8s/api and deployment.yaml. grep reports both as missing and the real file is never searched. The same happens with grep -l ... $(find k8s -name '*.yaml').

Yes. -exec ... {} + also passes many file names per call and handles spaces safely, with no pipe needed. xargs is still worth knowing because it adds options such as -P for parallel runs and -n to limit arguments per command.