grep searches for text patterns in files. It's how you find things inside files, not just file names.

Basic Usage

grep "pattern" filename

Shows lines containing the pattern.

Search a File

grep "error" log.txt

Shows every line containing "error".

Search Multiple Files

grep "pattern" file1.txt file2.txt
grep "pattern" *.txt

Shows matches with filenames.

Search Recursively (-r)

Search all files in a directory:

grep -r "pattern" .

The . means current directory. Goes through all subdirectories.

Case Insensitive (-i)

grep -i "error" log.txt

Matches "error", "Error", "ERROR", etc.

Show Line Numbers (-n)

grep -n "pattern" file.txt

Output:

12:This line contains pattern
45:Another pattern here

Show Only Filenames (-l)

grep -l "pattern" *.txt

Lists files that contain the pattern, not the matching lines.

Count Matches (-c)

grep -c "error" log.txt

Returns just the number of matches.

Invert Match (-v)

Show lines that DON'T contain the pattern:

grep -v "debug" log.txt

Filters out debug messages.

Whole Word Match (-w)

grep -w "error" log.txt

Matches "error" but not "errors" or "erroring".

Show Context

grep -A 2 "error" log.txt  # 2 lines After
grep -B 2 "error" log.txt  # 2 lines Before
grep -C 2 "error" log.txt  # 2 lines Context (both)

Helpful for understanding errors in context.

Regular Expressions

grep supports patterns:

Pattern Matches
. Any single character
* Zero or more of previous
^ Start of line
\$ End of line
[abc] Any of a, b, or c
[0-9] Any digit

Examples:

grep "^Error" log.txt     # Lines starting with "Error"
grep "error\$" log.txt     # Lines ending with "error"
grep "log[0-9]" file.txt  # log0, log1, log2, etc.

Extended Regex (-E)

For more patterns:

grep -E "error|warning" log.txt

Matches "error" OR "warning".

grep -E "[0-9]{3}-[0-9]{4}" file.txt

Matches phone number patterns like "555-1234".

Common Combinations

# Case insensitive, recursive, line numbers
grep -rni "pattern" .

# Search code, exclude directories
grep -r --exclude-dir=node_modules "pattern" .

# Search specific file types
grep -r --include="*.js" "function" .

Exclude Files and Directories

grep -r --exclude="*.log" "pattern" .
grep -r --exclude-dir=".git" "pattern" .
grep -r --exclude-dir={node_modules,dist,.git} "pattern" .

Practical Examples

Find a function in code:

grep -rn "function processData" --include="*.js" .

Find TODO comments:

grep -rni "TODO" --include="*.{js,py,rb}" .

Find errors in logs:

grep -i "error\|failed\|exception" /var/log/*.log

Search command history:

history | grep "docker"

Find files containing secrets (audit):

grep -rn "password\|api_key\|secret" --include="*.{js,json,py}" .

Colorized Output

grep --color=auto "pattern" file.txt

Make it default:

echo 'alias grep="grep --color=auto"' >> ~/.zshrc

Exit Status

grep returns:

  • 0 if matches found
  • 1 if no matches
  • 2 if error

Useful in scripts:

if grep -q "error" log.txt; then
    echo "Errors found!"
fi

The -q flag is quiet - no output, just the exit status.

Quick Reference

Command Result
grep "text" file Search file
grep -i "text" file Case insensitive
grep -r "text" . Recursive search
grep -n "text" file Show line numbers
grep -c "text" file Count matches
grep -l "text" *.txt List matching files
grep -v "text" file Invert match

Keep Learning

grep is essential for finding things in code. The free course covers search and more.

Check it out at Mac Terminal for Humans.