The Linux find command is a powerful tool for searching for files and directories in a directory hierarchy. It allows you to specify various criteria and actions to filter and manipulate the results.
In this article, you will learn how to use the -o and -r options in the find command to perform logical operations on the search expressions.
Table of Contents
What are the -o and -r Options?
The -o and -r options are used to combine multiple search expressions with logical operators. The -o option stands for “or” and the -r option stands for “reverse”. They have the following syntax:
find [path] [expression1] -o [expression2]
find [path] -r [expression]
The -o option returns all the files and directories that match either expression1 or expression2. The -r option returns all the files and directories that do not match the expression. For example, the following command will find all the files and directories that are either owned by the user “bob” or have the name “foo”:
find . -user bob -o -name foo
The following command will find all the files and directories that are not executable:
find . -r -executable
How are the -o and -r Options Different from the -a and ! Options?
The -a and ! options are also used to combine multiple search expressions with logical operators. The -a option stands for “and” and the ! option stands for “not”. They have the following syntax:
find [path] [expression1] -a [expression2]
find [path] ! [expression]
The -a option returns all the files and directories that match both expression1 and expression2. The ! option returns all the files and directories that do not match the expression. For example, the following command will find all the files and directories that are owned by the user “bob” and have the name “foo”:
find . -user bob -a -name foo
The following command will find all the files and directories that are not owned by the user “bob”:
find . ! -user bob
The -o and -r options are different from the -a and ! options in two ways:
The -o and -r options have lower precedence than the -a and ! options
This means that they are evaluated after the -a and ! options in the expression. For example, the following command will find all the files and directories that are either owned by the user “bob” or have the name “foo” and are executable:
find . -user bob -o -name foo -a -executable
This is equivalent to:
find . \( -user bob -o -name foo \) -a -executable
The parentheses are used to group the expressions and override the default precedence. If you want to find all the files and directories that are either owned by the user “bob” and executable or have the name “foo”, you need to use parentheses as follows:
find . \( -user bob -a -executable \) -o -name foo
The -o and -r options are not negated by the ! option
This means that the ! option does not affect the -o and -r options in the expression. For example, the following command will find all the files and directories that are not either owned by the user “bob” or have the name “foo”:
find . ! \( -user bob -o -name foo \)
This is equivalent to:
find . ! -user bob -a ! -name foo
The parentheses are used to group the expressions and negate them as a whole. If you use the ! option before the -o or -r option, it will not have any effect. For example, the following command will find all the files and directories that are either owned by the user “bob” or have the name “foo”, just like the previous example without the ! option:
find . ! -user bob -o -name foo
This is because the ! option only negates the -user bob expression, and the -o option returns the union of the results of the two expressions.
Conclusion
The -o and -r options are useful for performing logical operations on the search expressions in the find command. They allow you to specify complex criteria and filter the results according to your needs.
However, you need to be aware of their lower precedence and their non-negation by the ! option. You can use parentheses to group and override the default order of evaluation and negation.