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
Core Options
-rRecursive search. Searches all files in the current directory and subdirectories.
-iIgnore case. Treats 'Apple' and 'apple' as the same.
-vInvert match. Prints lines that DO NOT match the pattern.
-nShow line numbers along with matching lines.
-lShow only filenames of files containing the match.
Usage Examples
Basic Search
Find all lines containing 'error' in 'app.log'.
grep 'error' app.logCase 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.txtCount Occurrences
Count how many lines contain '404' in access.log.
grep -c '404' access.logSearch for Exact Word
Find 'is' but not 'this' or 'island' using word boundary.
grep -w 'is' story.txtShow Context
Show matching line plus 2 lines after (-A) and 2 lines before (-B).
grep -A 2 -B 2 'exception' error.log