next up previous contents index
Next: tarthe tape archiver Up: findthe file searcher Previous: Examples

A last word

Keep in mind that find is a very time consuming command, as it has to access each and every inode of the system in order to perform its operation. It is therefore wise to combine how many operations you need in a unique invocation of find, especially in the `housekeeping' jobs usually ran via a crontab job. A enlightening example is the following: let's suppose that we want to delete files ending in .BAK and change the protection of all directories to 771 and that of all files ending in .sh to 755. And maybe we are mounting NFS filesystems on a dial-up link, and we'd like not to check for files there. Why writing three different commands? The most effective way to accomplish the task is this:

% find . \( -fstype nfs -prune \) -o \
         \( -type d       -a -exec chmod 771 {} \; \) -o \
         \( -name "*.BAK" -a -exec /bin/rm {}   \; \) -o \
         \( -name "*.sh"  -a -exec chmod 755 {} \; \)

It seems ugly (and with much abuse of backslashes!), but looking closely at it reveals that the underlying logic is rather straightforward. Remember that what is really performed is a true/false evaluation; the embedded command is just a side effect. But this means that it is performed only if find must evaluate the exec part of the expression, that is only if the left side of the subexpression evaluates to true. So, if for example the file considered at the moment is a directory then the first exec is evaluated and the permission of the inode is changed to 771; otherwise it forgets all and steps to the next subexpression. Probably it's easier to see it in practice than to writing it down; but after a while, it will become a natural thing.

Converted on:
Mon Apr 1 08:59:56 EST 1996