# Basic search for "text" in a file
grep "text" filename.txt
# Case-insensitive search for "text" in a file
grep -i "text" filename.txt
# Recursive search for "text" in all files under the current directory
grep -r "text" .
# Recursive and case-insensitive search for "text"
grep -ri "text" .
# Count the number of lines that contain "text" in a file
grep -c "text" filename.txt
# Display line numbers while searching for "text" in a file
grep -n "text" filename.txt
# Search for lines that do NOT contain "text" in a file
grep -v "text" filename.txt
# Use extended regular expressions (ERE) with grep (e.g., search for "text" or "string")
grep -E "text|string" filename.txt
# Search for a whole word, not part of a word
grep -w "text" filename.txt
# Display only the names of files (with paths) that contain "text", without showing the matching lines
grep -rl "text" /path/to/search
# Exclude files matching a pattern (e.g., exclude all .log files)
grep --exclude=*.log -r "text" .
# Include files matching a specific pattern (e.g., search only .txt files)
grep --include=\*.txt -r "text" .
# Search for "text" in all files, but exclude searching in directories named "cache"
grep -r --exclude-dir=cache "text" .
# Use grep with output from another command (search for "text" in the list of running processes)
ps aux | grep "text"
# Search for a pattern that starts with a hyphen (-) by using -- before the pattern
grep -- "-text" filename.txt