UNIX Tip of the Week

find


Find is one of the most useful commands in the entire Unix operating system, yet its slightly unusual syntax keeps many from enjoying its utility. The find command has a three-part syntax:

find   <where to start finding>   <what to find>   <what to do once it's found>

An example:    find /home/support/marty -size +1000k -print

This will start looking in my home directory for files larger than 1 MB (1000 kB) and will print them to standard output once it finds them. Had I specified "-1000k" instead of "+1000k" it would look for files smaller than 1 MB.

Make certain that special shell characters are escaped:    find . -name \*tex -print

The possibilities are endless; You can tell find whether or not to cross filesystems, NFS mount points, or whether or not to follow symbolic links. You can specify the "what to find" section as a boolean string, and can also operate on the files it finds with the -exec option:

find . -type f \( -mtime -90 -a -size +1000k \) -exec rm {} \;
This finds plain files that haven't been accessed in any way (not even read) for at least ninety days, and (-a) whose size is at least 1MB. It then removes these files.

The curly braces {} equates to the output of find. The above example might read "remove whatever find finds.
The escaped semiclon \; is a long story. It's needed when doing an -exec.


The above has barely scratched the surface of what may be done with find. This page's purpose is twofold:

  1. To aquaint you with the three-part syntax of find
  2. To whet your curiosity about other things that find is good for
And there are plenty, believe me. I know I always (with tongue-in-cheek) refer you to the man page, but really, this time. It's a well-written, easy to understand page that illustrates the richness of this utility.


Revised: April 17, 2000