AZ.dev

Run a command on every file find matches

| posix: any gnu-findutils: any

To run a command on each file find matches, use -exec with {} as the placeholder and a terminator.

The Fix

find . -name "*.log" -exec rm {} \;

For thousands of files, batch them into fewer invocations with + instead of \;:

find . -name "*.log" -exec rm {} +

Why {} and \; Are Needed

{} gets replaced with the matched file’s path. find needs a terminator to know where the -exec command ends, and ; is a shell special character, so it must be escaped (\;) or quoted (';'). Ending with + instead runs the command once with as many matched paths appended as fit, like xargs does, rather than once per file.

Gotchas

find exec shell files unix xargs

Was this helpful?

Related Entries

Get the full (absolute) path of a fileFind files modified within a certain time range