Here's something that will really make your head spin. Remember that -exec doesn't necessarily evaluate to "true"; it only evaluates to true if the command it executes returns a. You can use this to construct custom find tests.
Assume that you want to list files that are "beautiful." You have written a program called beauty that returns zero if a file is beautiful, and non-zero otherwise. (This program can be a, a script, an executable from a C program, or anything you like.)
Here's an example:
In this command, -exec
is just another find
The only difference is that we care about its value; we're not
assuming that it will always be "true." find
command for every file. Then -exec
to true when find
is looking at a "beautiful" program, causing
to print the filename. (Excuse me, causing find
evaluate the -print
Of course, this ability is capable of infinite variation. If you're interested in finding beautiful C code, you could use the command:
And so on. For performance reasons, it's a good idea to put the -exec operator as close to the end as possible. This avoids starting processes unnecessarily; the -exec command will execute only when the previous operators evaluate to true.