LinuxCommandLibrary

grep

TLDR

Search for pattern in file

$ grep [pattern] [file]
copy
Case insensitive search
$ grep -i [pattern] [file]
copy
Recursive search in directory
$ grep -r [pattern] [directory]
copy
Show line numbers
$ grep -n [pattern] [file]
copy
Invert match (exclude pattern)
$ grep -v [pattern] [file]
copy
Extended regex
$ grep -E '[regex]' [file]
copy
Count matches
$ grep -c [pattern] [file]
copy

SYNOPSIS

grep [options] pattern [files]

DESCRIPTION

grep searches files for lines matching a pattern. It's one of the most fundamental Unix utilities, named for g/re/p (global regular expression print) from ed.
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.
grep is essential for text searching and filtering.

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.
--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 1974. It was inspired by the g/re/p command in ed. GNU grep is the most widely used implementation today.

SEE ALSO

egrep(1), fgrep(1), rg(1), ack(1), sed(1)

Copied to clipboard