Solaris Advanced User's Guide

Searching for 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 file names that match a certain pattern or that have been modified within a specified time frame.

Unlike most commands, find options are several characters long. You must specify the starting directory before your desired options.

$ find directory options

In the previous example, 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. The more options you apply, the narrower the field becomes. The -print option indicates that you want the system to display the results.

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/bin/calendar is calendar. This portion of a file's name is often called the base name.

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

$ find . -name '*s' -print

The following table describes other options of the find command.

Table 3–1 find Options



-name filename

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

-user userid

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

-group group

Selects files that belong to group.

-m -time n

Selects files that have been modified within n days.

-newer checkfile

Selects files that have been modified more recently than checkfile.


You can specify an order of options 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

In the previous example, the find command searches in the . directory for all files that are named AAA, then looks for all files named BBB. find then displays the results of both searches.

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

$ find . \!-name BBB -print

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

-exec command '{}' \;

You terminate this option with an escaped semicolon (\;). The quoted braces are replaced with the file names that find selects.

You can use find to automatically remove temporary work files. If you name your temporary files consistently, you can use find to search for and remove these files. For example, if you name your temporary files junk or dummy, this command finds and removes the files.

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

For more information on searching for files, see the man page find(1).