Skip to content

Q&A · Answered

How to search text in files using grep

asked 9 months, 2 weeks ago 14645 views 1 answer
14

I need to find files containing specific text. How do I use grep effectively?

1 Answer

One accepted
9

Accepted answer

Not confirmed since it was written, 9 months, 2 weeks ago
# 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.txt

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.