grep

Global Regular Expression Print. Searches for patterns in files. It is one of the most powerful text search tools available in the Unix toolbox.

Synopsis

grep [options] 'pattern' file(s)

Core Options

-r

Recursive search. Searches all files in the current directory and subdirectories.

-i

Ignore case. Treats 'Apple' and 'apple' as the same.

-v

Invert match. Prints lines that DO NOT match the pattern.

-n

Show line numbers along with matching lines.

-l

Show only filenames of files containing the match.

Usage Examples

Basic Search

Find all lines containing 'error' in 'app.log'.

grep 'error' app.log

Case Insensitive Recursive Search

Find 'todo' in all files in the current directory and subdirectories, ignoring case.

grep -ri 'todo' .

Invert Match

Show lines in 'config.txt' that do NOT start with '#'.

grep -v '^#' config.txt

Count Occurrences

Count how many lines contain '404' in access.log.

grep -c '404' access.log

Search for Exact Word

Find 'is' but not 'this' or 'island' using word boundary.

grep -w 'is' story.txt

Show Context

Show matching line plus 2 lines after (-A) and 2 lines before (-B).

grep -A 2 -B 2 'exception' error.log
Built for builders.