Skip to content
Latchkey

find -exec: Run a Command on Each Match

find -exec runs a command for the files it matches, substituting each path for {}.

When -delete or a simple action is not enough, -exec runs any command on every match. The terminator you choose changes how it batches.

What it does

find -exec CMD {} \; runs CMD once per matched file, replacing {} with the path. The expression must end with a literal semicolon, which is escaped as \; so the shell does not consume it. The {} stands in for the current file name.

Common usage

Terminal
find . -name '*.tmp' -exec rm -f {} \;
find . -name '*.sh' -exec chmod +x {} \;
find . -name '*.json' -exec jq . {} \;

Options

ElementWhat it does
-exec CMD {} \;Run CMD once per file, {} = the path
{}Placeholder replaced by the current file name
\;Terminator: one invocation per file
-execdir CMD {} \;Run from the file directory (safer paths)
-ok CMD {} \;Like -exec but prompts before each run

In CI

Per-file -exec ... \; spawns one process per match, which is slow for thousands of files; use the + terminator to batch them. -execdir runs the command in each file directory, which avoids surprises when names contain leading dashes.

Common errors in CI

"find: missing argument to -exec" means the terminator is missing or unescaped; the expression must end with \; (or +). "find: -exec: no terminating ';' or '+'" is the same problem. A bare ; gets eaten by the shell, so always escape it as \; or quote it.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →