GNU Info

Info Node: (find.info)Adding Tests

(find.info)Adding Tests


Prev: Run Commands Up: Actions
Enter node , (file) or (file)node

Adding Tests
============

   You can test for file attributes that none of the `find' builtin
tests check.  To do this, use `xargs' to run a program that filters a
list of files printed by `find'.  If possible, use `find' builtin tests
to pare down the list, so the program run by `xargs' has less work to
do.  The tests builtin to `find' will likely run faster than tests that
other programs perform.

   For example, here is a way to print the names of all of the
unstripped binaries in the `/usr/local' directory tree.  Builtin tests
avoid running `file' on files that are not regular files or are not
executable.

     find /usr/local -type f -perm +a=x | xargs file |
       grep 'not stripped' | cut -d: -f1

The `cut' program removes everything after the file name from the
output of `file'.

   If you want to place a special test somewhere in the middle of a
`find' expression, you can use `-exec' to run a program that performs
the test.  Because `-exec' evaluates to the exit status of the executed
program, you can write a program (which can be a shell script) that
tests for a special attribute and make it exit with a true (zero) or
false (non-zero) status.  It is a good idea to place such a special
test _after_ the builtin tests, because it starts a new process which
could be avoided if a builtin test evaluates to false.  Use this method
only when `xargs' is not flexible enough, because starting one or more
new processes to test each file is slower than using `xargs' to start
one process that tests many files.

   Here is a shell script called `unstripped' that checks whether its
argument is an unstripped binary file:

     #! /bin/sh
     file $1 | grep 'not stripped' > /dev/null

   This script relies on the fact that the shell exits with the status
of the last program it executed, in this case `grep'.  `grep' exits
with a true status if it found any matches, false if not.  Here is an
example of using the script (assuming it is in your search path).  It
lists the stripped executables in the file `sbins' and the unstripped
ones in `ubins'.

     find /usr/local -type f -perm +a=x \
       \( -exec unstripped '{}' \; -fprint ubins -o -fprint sbins \)


automatically generated by info2www version 1.2.2.9