Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux grep command

Reference for the Linux grep command: search files and streams with patterns, use -i -v -n -c -r -o -w -A -B -C, understand exit status and avoid common mistakes.

grep prints the lines of its input that match a pattern. The input can be files, a whole directory tree (with -r) or standard input, which makes grep the usual last stage of a pipeline: ps aux | grep nginx. The name stands for "global regular expression print".

By default the pattern is a POSIX basic regular expression (BRE). -E switches to extended syntax and -F to plain fixed strings; both have their own page: grep -E and grep -F. The exit status makes grep equally useful in if statements.

Terminal
grep [OPTION]... PATTERN [FILE]...

Try it

Bash Everyday options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Context, recursion and exit status

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
grep [OPTION]... PATTERN [FILE]...
grep [OPTION]... -e PATTERN... [FILE]...
grep [OPTION]... -f PATTERNFILE [FILE]...

Without a FILE, grep reads standard input; with -r and no file it searches the current directory.

Option Description
-i Ignore case.
-v Invert: print lines that do not match.
-n Prefix each line with its line number.
-c Print only a count of matching lines per file.
-l, -L Print only names of files with (or without) a match.
-o Print only the matching parts, one per line.
-q Quiet: print nothing, exit at the first match.
-s Suppress errors about missing or unreadable files.
-w Match whole words only.
-x Match whole lines only.
-A N, -B N, -C N Print N lines of context after, before, or around each match.
-m N Stop after N matching lines.
-r, -R Search directories recursively (-R follows all symlinks).
--include=GLOB, --exclude=GLOB, --exclude-dir=GLOB Limit which files a recursive search visits.
-H, -h Always / never print file names.
-e PATTERN Give the pattern explicitly; repeat for alternatives. Needed when the pattern starts with -.
-f FILE Read patterns from FILE, one per line.
-E, -F, -P Extended regex, fixed strings, Perl-compatible regex (if built in).
-a Treat binary files as text.
-z Lines end with NUL, not newline.
--color=auto Highlight matches on a terminal.

Returns: 0 if at least one line was selected, 1 if none was, 2 if an error occurred (unless -q and a match was found, which still gives 0).

Usage notes

  • Quote the pattern. grep 'a.*b' file keeps the shell from expanding *, ? and [. Single quotes are safest; use double quotes only when you need a variable inside.
  • BRE specials. In basic regexes +, ?, |, (, ) and { are literal characters; their special forms are \+, \?, \|, \(, \), \{. GNU grep supports those escapes. For a readable pattern use -E.
  • Pattern that starts with a dash: grep -e -v file or grep -- -v file, otherwise it is read as an option.
  • -c counts lines, not matches. To count every occurrence use grep -o pattern file | wc -l.
  • Multiple files add the file name prefix automatically; add -h to remove it or -H to force it for one file.
  • Binary files: grep prints Binary file x matches instead of the line. -a forces text, -I skips binary files in recursive searches.
  • Do not use cat file | grep; grep pattern file is shorter and lets grep print file names and handle errors.
  • ps | grep name matches itself. Use pgrep, or the trick grep '[n]ame'.
  • Speed: -F and a locale of LC_ALL=C make large searches much faster. -m 1 or -q stop at the first match.
  • With set -e/pipefail, remember that "no match" is exit status 1, which a script may treat as failure; write grep ... || true when that is acceptable.
  • Regular expressions: the full syntax is in the regular expressions lesson.