Perhaps the most powerful action available in find is -exec, which allows you to run a command on each of the found results. This is best explained by an example; this command looks for occurrences of the search string in all files with names ending in .vim in a directory named vim:
$ find vim -type f -name '*.vim' -exec grep -F search -- {} \;
The -exec action here is followed by the name of the grep tool, its -F option, a "search" pattern, an option terminator (--), and two special arguments:
- {}: Replaced with each find result
- \;: Terminates the command
Suppose find found three matching files with the conditions we gave it:
$ find vim -type f -name '*.vim' vim/config/main.vim vim/maps.vim vim/patterns.vim
Adding the -exec command line does the same thing as if our grep command were run over each of those files. It's like running these three commands:
$ grep -F search -- vim/config/main.vim $ grep -F search -- vim/maps/vim $ grep -F search -- vim/patterns/vim
If we change the command terminator to a plus sign (+), we can change the behavior to put as many of the arguments as possible on one line. This can be easier and more efficient for programs that can accept more than one filename argument:
$ find vim -type f -name '*.vim' -exec grep -F search -- {} +
The preceding command is like running this:
$ grep -F search -- vim/config/main.vim vim/maps/vim vim/patterns/vim