Skip to content

grep

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.

Syntax

grep [options] pattern [file]

Options

  • -i: Ignore case distinctions in both the PATTERN and the input files.
  • -v: Invert the sense of matching, to select non-matching lines.
  • -c: Suppress normal output; instead print a count of matching lines for each input file.
  • -n: Prefix each line of output with the 1-based line number within its input file.
  • -l: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
  • -L: Suppress normal output; instead print the name of each input file from which no output would normally have been printed.
  • -r: Read all files under each directory, recursively.
  • -w: Select only those lines containing matches that form whole words.
  • -x: Select only those matches that exactly match the whole line.
  • -A: Print NUM lines of trailing context after matching lines.
  • -B: Print NUM lines of leading context before matching lines.
  • -C: Print NUM lines of output context.
  • -E: Interpret PATTERN as an extended regular expression.
  • -F: Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
  • -G: Interpret PATTERN as a basic regular expression.
  • -P: Interpret PATTERN as a Perl-compatible regular expression.
  • -o: Show only the part of a line matching PATTERN.
  • -q: Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
  • -s: Suppress error messages about nonexistent or unreadable files.
  • -h: Suppress the prefixing of filenames on output when multiple files are searched.
  • -H: Print the file name for each match.
  • -Z: Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name.
  • -z: Output a zero byte (the ASCII NUL character) after the name of the input file.
  • --binary-files: Without this option, grep takes binary files as text files and prints the matching lines.
  • --color: Highlight the matching strings.
  • --exclude: Skip files whose base name matches the specified pattern.
  • --exclude-dir: Skip directories whose base name matches the specified pattern.
  • --include: Search only files whose base name matches the specified pattern.
  • --exclude-from: Skip files whose base name matches any of the file-name globs read from file.
  • --exclude-dir-from: Skip directories whose base name matches any of the file-name globs read from file.
  • --include-from: Search only files whose base name matches any of the file-name globs read from file.
  • --line-buffered: Force output to be line buffered.
  • --mmap: Use the mmap(2) system call to read input, instead of the default read(2) system call.
  • --no-group-separator: Suppress the prefixing of filenames on output when multiple files are searched.
  • --null: Output a zero byte (the ASCII NUL character) after the file name.
  • --null-data: Treat input files as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
  • --label: Display input actually coming from standard input as input coming from file LABEL.
  • --no-filename: Suppress the prefixing of filenames on output when multiple files are searched.
  • --with-filename: Print the file name for each match.
  • --line-number: Prefix each line of output with the 1-based line number within its input file.
  • --word-regexp: Select only those lines containing matches that form whole words.
  • --files-with-matches: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
  • --files-without-match: Suppress normal output; instead print the name of each input file from which no output would normally have been printed.
  • --count: Suppress normal output; instead print a count of matching lines for each input file.
  • --max-count: Stop reading a file after NUM matching lines.
  • --quiet: Suppress all normal output.
  • --silent: Suppress all normal output.
  • --binary: Treat the file(s) as binary.
  • --text: Treat the file(s) as text.
  • --help: Display a help message, and exit.
  • --version: Display version information, and exit.

Examples

Grep for a string in a file

grep "string" file.txt

Grep for a string in multiple files

grep "string" file1.txt file2.txt

Grep for a string in all files in a directory

grep "string" *

Grep for a string in all files in a directory recursively

grep -r "string" *

Grep for a string in all files in a directory recursively with line numbers

grep -rn "string" *

Grep for a string in all files in a directory recursively with line numbers and file names

grep -rnl "string" *

Grep for a string in all files in a directory recursively with line numbers and file names and ignore case

grep -rnli "string" *

Grep for a string in all files in a directory recursively with line numbers and file names and ignore case and show only the part of a line matching PATTERN

grep -rnlio "string" *

Grep for a string in all files in a directory recursively with line numbers and file names and ignore case and show only the part of a line matching PATTERN and highlight the matching strings

grep -rnlio --color "string" *

Grep for a string in all files in a directory recursively with line numbers and file names and ignore case and show only the part of a line matching PATTERN and highlight the matching strings and show only the part of a line matching PATTERN

grep -rnlio --color -o "string" *

Zgrep

Zgrep is identical to grep, but it decompresses files before searching.

Syntax

zgrep [options] pattern [file]

Options

Same as grep.