how to run find with -exec

UNIX allows users to pipe each utility’s output to another utility’s input to accomplish complicated tasks.

Find command with -exec

1
$ find . -name '*.txt' -exec grep -i apple {} ;

We first find txt file in this directory. Then, execute grep and substitute {} with the results we just found. That is, the filenames.

Result

1
I like to eat apples.

If we want to display the filename the matched lines are in, we can change the command above to

1
$ find . -name '*.txt' -exec grep -i apple {} +;