find’s -exec feature

A seemingly underused feature of find is its -exec feature. Undoubtedly, the main reason for this is the rather confusing covereage of this feature in find’s man page, which I reproduce for you here:

-exec utility [argument ...];

True if the program named utility returns a zero value as its
exit status. Optional arguments may be passed to the utility.
The expression must be terminated by a semicolon (“;”). If the
string “{}” appears anywhere in the utility name or the argu-
ments it is replaced by the pathname of the current file.
Utility will be executed from the directory from which find was
executed. Utility and arguments are not subject to the further
expansion of shell patterns and constructs.

Whenever I needed to do such a task, I found myself fumbling with xargs, pipes, outputting to a file and manipulating the resultant text with other programs, etc. I always looked at -exec with great hope and wonder, but always walked away thinking it did not offer me the functionality I seeked. I admit, looking at the man page now, it clearly describes exactly what -exec does and how to use it. But I swear, I read that thing 10 times over the course of 5 years and it never clicked. I think what threw me was the first sentence, which made me gather that it was some sort of tool to do an audit of a boolean characteristic of each file in the search results. Referring to a program/command/binary as a “utility” also doesn’t really help things.

But recently I found a page on the web, I believe it was this one, that showed me the -exec flag does exactly what I need.

The key is knowing that {} should be placed where each result of find should be placed in the desired command, and the series of flags should be terminated with \;.

For example, If you want to check the syntax on each php file in a tree by executing php -l on each, you would use this command:

find . -name "*.php" -exec php -l {} \;

Awesome.

4 Responses to “find’s -exec feature”


  1. 1 jeff

    Very nice. I remember having used this flag years ago, probably in some slick tutorial article, but I had forgotten all about it. Maybe this would make a good alias for something like “checkPHP”, and similar variants? (I find that my .tcshrc file ends up also being a crib sheet for lesser-used unix pipings.)

  2. 2 josh

    I bow to your ‘find’ prowess

  3. 3 andrewz

    Yeah, find is brutally powerful, and sometimes difficult to master. I used it extensively when I was an interning as a software archive administrator at a baby bell telco. I used various testbeds to proof my wacky shell scripts before unleashing them on the massive VAX/BSD code trees. These scripts would run for hours.

    In subsequent years I have found that my grasp of the power and subtleties of find and exec have seriously wained in disuse.

    - Andrew

  4. 4 Mark

    Thanks for the post. Escaping the ; with a backslash was killing me. Also, use a + if your exec command handles it and it will seriously speed things up.

Leave a Reply




Close
Powered by ShareThis