2.1.3 General Output Control


Suppress normal output; instead print a count of matching lines for each input file. With the -v (--invert-match) option, count non-matching lines. (-c is specified by POSIX.)


Surround matched non-empty strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS and default to ‘ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36’ for bold red matched text, magenta file names, green line numbers, green byte offsets, cyan separators, and default terminal colors otherwise. See Environment Variables.

WHEN is ‘always’ to use colors, ‘never’ to not use colors, or ‘auto’ to use colors if standard output is associated with a terminal device and the TERM environment variable’s value suggests that the terminal supports colors. Plain --color is treated like --color=auto; if no --color option is given, the default is --color=never.


Suppress normal output; instead print the name of each input file from which no output would normally have been printed.


Suppress normal output; instead print the name of each input file from which output would normally have been printed. Scanning each input file stops upon first match. (-l is specified by POSIX.)

-m num

Stop after the first num selected lines. If num is zero, grep stops right away without reading input. A num of −1 is treated as infinity and grep does not stop; this is the default.

If the input is standard input from a regular file, and num selected lines are output, grep ensures that the standard input is positioned just after the last selected line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. For example, the following shell script makes use of it:

while grep -m 1 'PATTERN'
  echo xxxx
done < FILE

But the following probably will not work because a pipe is not a regular file:

# This probably will not work.
cat FILE |
while grep -m 1 'PATTERN'
  echo xxxx

When grep stops after num selected lines, it outputs any trailing context lines. When the -c or --count option is also used, grep does not output a count greater than num. When the -v or --invert-match option is also used, grep stops after outputting num non-matching lines.


Print only the matched non-empty parts of matching lines, with each such part on a separate output line. Output lines use the same delimiters as input, and delimiters are null bytes if -z (--null-data) is also used (see Other Options).


Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. Portability note: Solaris 10 grep lacks -q; portable shell scripts typically can redirect standard output to /dev/null instead of using -q. (-q is specified by POSIX.)


Suppress error messages about nonexistent or unreadable files. (-s is specified by POSIX.)