Script Valley
Linux & Bash for Developers
Text Processing & SearchingLesson 2.2

grep command tutorial for searching files

grep syntax, -i -v -r -n -c flags, regex basics, egrep, fixed strings, grep with pipes, color output

grep: Find Lines That Match a Pattern

grep filters lines from a file (or stdin) that match a pattern. It is your primary tool for digging through logs, searching codebases, and filtering command output.

Essential grep Flags

-i makes matching case-insensitive. -v inverts the match (show lines that do NOT match). -r recurses through directories. -n shows line numbers. -c counts matching lines instead of printing them. -l prints only the filenames that match, not the lines.

# Find all ERROR lines in a log
grep "ERROR" /var/log/app.log

# Case-insensitive search
grep -i "warning" /var/log/app.log

# Show line numbers
grep -n "404" /var/log/nginx/access.log

# Search recursively in all .js files
grep -r "console.log" ./src --include="*.js"

# Lines that do NOT contain 200 (non-successful HTTP)
grep -v " 200 " /var/log/nginx/access.log

# Count how many times ERROR appears
grep -c "ERROR" app.log

grep with Pipes

grep becomes powerful when combined with other commands. Pipe any output into grep to filter it inline โ€” you do not need to write to a file first.

# Find running nginx processes
ps aux | grep nginx

# Search command history
history | grep "git commit"

Up next

How to use pipes and redirection in Bash

Sign in to track progress

grep command tutorial for searching files โ€” Text Processing & Searching โ€” Linux & Bash for Developers โ€” Script Valley โ€” Script Valley