14
I need to find files containing specific text. How do I use grep effectively?
Loading...
Q&A · Answered
I need to find files containing specific text. How do I use grep effectively?
Accepted answer
# Basic search
grep "pattern" file.txt
# Recursive search in directory
grep -r "pattern" /path/
# Case insensitive
grep -i "pattern" file.txt
# Show line numbers
grep -n "pattern" file.txt
# Show only filenames
grep -l "pattern" *.txt
# Invert match (lines NOT matching)
grep -v "pattern" file.txt
# Regex search
grep -E "error|warning" logfile.log
# Count matches
grep -c "pattern" file.txtSay what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.