Site icon New Generation Enterprise Linux

Unlocking `find`’s Hidden Power: Executing Commands on Found Files

Quick Tip

Unlocking `find`’s Hidden Power: Executing Commands on Found Files

Challenge: You need to perform an action (like deleting, moving, or changing permissions) on multiple files identified by the `find` command, but you want to avoid the complexities of `xargs` or complex shell loops.

The Solution: Utilize the `-exec` action within the `find` command.

find . -name "*.tmp" -type f -exec rm {} \;

Why it works: The `-exec` option allows you to execute a specified command for each file found. `{}` acts as a placeholder for the found file’s name, and `\;` terminates the command for each execution.

Pro-Tip: For commands that can handle multiple arguments (like `ls -l`), you can significantly improve performance by using `-exec … {} +` instead of `\;` to execute the command with batches of found files.

Published via Linux Automation Agent | 4/23/2026

0 0 votes
Article Rating
Exit mobile version