OpenWindows Advanced User's Guide

3.5 Looking Up Files (find)

The find command searches for files that meet conditions you specify, starting from a directory you name. For example, you might search for filenames that match a certain pattern or that have been modified within a specified time frame.

Unlike most commands, find options are several characters long, and the name of the starting directory must precede them on the command line as follows:

$ find directory options

where directory is the name of the starting directory and options represents the options for the find command.

Each option describes a criterion for selecting a file. A file must meet all criteria to be selected. Thus, the more options you apply, the narrower the field becomes. The -print option indicates that you want the results to be displayed. (As described later on, you can use find to run commands. You may want find to omit the display of selected files in that case.)

The -name filename option tells find to select files that match filename. Here filename is taken to be the rightmost component of a file's full path name. For example, the rightmost component of the file /usr/lib/calendar is calendar. This portion of a file's name is often called its base name.

For example, to see which files within the current directory and its subdirectories end in s, type:

$ find . -name '*s' -print
./programs
./programs/graphics
./programs/graphics/gks
./src/gks
$

Other options include:

-name filename

Selects files whose rightmost component matches filename. Surround filename with single quotes if it includes filename substitution patterns.

-user userid

Selects files owned by userid. userid can be either a login name or user ID number.

-group group

Selects files belonging to group.

-m -time n

Selects files that have been modified within n days.

-newer checkfile

Selects files modified more recently than checkfile.

You can specify an order of precedence by combining options within (escaped) parentheses (for example, \(options\) ). Within escaped parentheses, you can use the -o flag between options to indicate that find should select files that qualify under either category, rather than just those files that qualify under both categories:

$ find . \( -name AAA -o -name BBB \) -print
./AAA
./BBB

You can invert the sense of an option by prepending an escaped exclamation point. find then selects files for which the option does not apply:

$ find . \!-name BBB -print
./AAA

You can also use find to apply commands to the files it selects with the

-exec command '{}' \;

option. This option is terminated with an escaped semicolon (\;). The quoted braces are replaced with the filenames that find selects.

As an example, you can use find to automatically remove temporary work files. If you name your temporary files consistently, you can use find to seek them out and destroy them wherever they lurk. For example, if you name your temporary files junk or dummy, this command will find them and remove them:

$ find . \( -name junk -o -name dummy \) -exec rm '{}' \;

For more information on find(1), refer to the man Pages(1): User Commands.