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.
grep [OPTION]... PATTERN [FILE]...
Try it
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.
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
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' filekeeps 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 fileorgrep -- -v file, otherwise it is read as an option. -ccounts lines, not matches. To count every occurrence usegrep -o pattern file | wc -l.- Multiple files add the file name prefix automatically; add
-hto remove it or-Hto force it for one file. - Binary files: grep prints
Binary file x matchesinstead of the line.-aforces text,-Iskips binary files in recursive searches. - Do not use
cat file | grep;grep pattern fileis shorter and letsgrepprint file names and handle errors. ps | grep namematches itself. Usepgrep, or the trickgrep '[n]ame'.- Speed:
-Fand a locale ofLC_ALL=Cmake large searches much faster.-m 1or-qstop at the first match. - With
set -e/pipefail, remember that "no match" is exit status 1, which a script may treat as failure; writegrep ... || truewhen that is acceptable. - Regular expressions: the full syntax is in the regular expressions lesson.
Related
grep -Eandgrep -F,sed,awk,cut- Lessons: grep, Regular expressions, Pipes