HxHippy

grep

Print lines matching a pattern

Overview

The grep command searches for PATTERNS in each FILE. A PATTERN is a sequence of characters that can be a fixed string or a regular expression. grep prints each line that matches a pattern.

Syntax

grep [OPTIONS] PATTERN [FILE...]

Common Options

-i, --ignore-case

Ignore case distinctions in patterns and input data.

-v, --invert-match

Invert the sense of matching, to select non-matching lines.

-n, --line-number

Prefix each line of output with the 1-based line number within its input file.

-r, -R, --recursive

Read all files under each directory, recursively. Follow symbolic links on the command line, but skip symlinks encountered recursively.

-l, --files-with-matches

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

-c, --count

Suppress normal output; instead print a count of matching lines for each input file.

-E, --extended-regexp

Interpret PATTERN as an extended regular expression (ERE).

-F, --fixed-strings

Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

-w, --word-regexp

Select only those lines containing matches that form whole words.

Examples

$ grep "error" logfile.txt

Search for the word "error" in logfile.txt.

$ grep -i "hello" myfile.txt

Search for "hello" (case-insensitive) in myfile.txt.

$ grep -r "config_value" /etc/

Recursively search for "config_value" in all files under /etc/.

$ grep -v "^#" config.conf

Display all lines from config.conf that do not start with # (comments).

$ grep -l "main" *.c

List all C files (*.c) that contain the word "main".

$ ps aux | grep "firefox"

Find processes related to firefox.

searchpatternfindtextfilterregexgrep