grep
Pattern-matching text search utility
TLDR
Search for pattern in file
$ grep [pattern] [file]
Case insensitive search$ grep -i [pattern] [file]
Recursive search in directory$ grep -r [pattern] [directory]
Show line numbers$ grep -n [pattern] [file]
Invert match (exclude pattern)$ grep -v [pattern] [file]
Extended regex$ grep -E '[regex]' [file]
Show only filenames containing matches$ grep -rl [pattern] [directory]
Count matches$ grep -c [pattern] [file]
Search with context (3 lines before and after)$ grep -C 3 [pattern] [file]
Search recursively but only in specific file types$ grep -r --include='[*.py]' [pattern] [directory]
SYNOPSIS
grep [options] pattern [files]
DESCRIPTION
grep searches files for lines matching a regular expression pattern. It is one of the most fundamental Unix utilities, named for g/re/p (global regular expression print) from the ed editor.The tool supports basic and extended regular expressions, recursive directory searching, and various output formats. It can search multiple files, show context around matches, and highlight results with color.
PARAMETERS
PATTERN
Regular expression pattern to match.FILES
Files to search.-i, --ignore-case
Case insensitive matching.-v, --invert-match
Select non-matching lines.-r, --recursive
Search directories recursively.-n, --line-number
Show line numbers.-c, --count
Print match count only.-l, --files-with-matches
Print only filenames.-E, --extended-regexp
Use extended regex.-F, --fixed-strings
Match literal strings.-o, --only-matching
Print only matched parts.-A NUM
Print NUM lines after match.-B NUM
Print NUM lines before match.-C NUM
Print NUM lines of context.-P, --perl-regexp
Use Perl-compatible regular expressions (PCRE).-w, --word-regexp
Match only whole words.-q, --quiet, --silent
Suppress output; exit with 0 if match found.--include GLOB
Search only files matching the glob pattern.--exclude GLOB
Skip files matching the glob pattern.--color
Highlight matches.--help
Display help information.
CAVEATS
Basic vs extended regex syntax differences. Binary files may produce unexpected output. Large files may be slow without optimizations.
HISTORY
grep was created by Ken Thompson at Bell Labs in 1973. It was inspired by the g/re/p command in ed. GNU grep is the most widely used implementation today.
