Logical Condition Operators
Understanding Logical Operators in Find Command
Logical operators in the find
command allow you to combine multiple search conditions, creating complex and precise file searches.
Key Logical Operators
1. AND Operator (-a)
The AND operator combines multiple conditions, returning files that match ALL specified criteria.
## Find files named "example" and larger than 10KB
find / -name "example" -a -size +10k
2. OR Operator (-o)
The OR operator returns files matching ANY of the specified conditions.
## Find files with .txt or .log extensions
find / \( -name "*.txt" -o -name "*.log" \)
3. NOT Operator (!)
The NOT operator excludes files matching a specific condition.
## Find files not owned by root
find / ! -user root
Logical Operator Flowchart
graph TD
A[Search Conditions] --> B{Logical Operator}
B --> |AND| C[All Conditions Must Match]
B --> |OR| D[Any Condition Can Match]
B --> |NOT| E[Exclude Matching Conditions]
Complex Search Examples
Multiple Condition Search
## Find text files larger than 1MB, modified in last 7 days
find / -type f -name "*.txt" -size +1M -mtime -7
Operator Precedence and Grouping
Conditions can be grouped using parentheses \( \)
to control search logic.
## Complex search with grouped conditions
find / \( -name "*.log" -o -name "*.txt" \) -a ! -path "*/temp/*"
Comparison of Logical Operators
Operator |
Symbol |
Description |
Example |
AND |
-a |
All conditions must match |
-name "*.txt" -a -size +10k |
OR |
-o |
Any condition can match |
-name ".txt" -o -name ".log" |
NOT |
! |
Exclude matching conditions |
! -user root |
- Use specific paths to limit search scope
- Combine conditions efficiently
- Avoid overly complex searches
LabEx recommends practicing these operators in a controlled environment to master their usage.