Combinations of expressions



Last revision July 20, 2004

Additional topics:

Expressions are normally evaluated from left to right, each one further restricting the set of files to be passed onto the next expression. This is a logical "and" operation. Example:

find dirlist -name '*.f' -mtime +7 -print

Here, all the files in the directories listed in dirlist are first matched against the -name expression; if a file passes that selection expression, it is then matched against the -mtime expression. Finally, the files that pass both selection expressions are passed to the -print expression, which lists their names to the terminal (standard output).

Expressions may also be joined by a logical "or" test, in which case only one needs to match in order to select the file for the next expression.

Indicate a logical "or" operation by putting -o between the expressions, for example,

-name '*.f' -o -name '*.o'

Use escaped parentheses \( and \) to group combinations of expressions. This is necessary with three or more expressions to be sure that the "or" and "and" operations are tested in the correct order. Example:

find dirlist \( -name '*.f' -o -name '*.c' \) -mtime +7 -print

Finally, you can negate any single expression by putting an escaped exclamation point (\!) ahead of it. This means that the file is to be selected if it does not match the expression. Example:

-name '*.f' \! -name 'test*'

This selects any file whose filename ends in .f and does not start with test.