Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use grep to find lines that match a pattern in a file or in command output. The basic form is grep [options] 'PATTERN' [FILE...]; leave off the file names to read from standard input. For literal text, choose -F; for patterns, use the default basic regular expressions or select -E for extended regular expressions.

Search a file or command output

GNU grep searches input for lines containing a match and prints those lines by default. Its documented command form is grep [OPTION]... PATTERN [FILE...]. Quote the pattern so the shell passes characters such as *, ., and | to grep rather than interpreting them first.

# Find lines containing error in one file
grep 'error' app.log

# Search multiple files and show line numbers
grep -n 'timeout' app.log server.log

# Ignore letter case
grep -i 'warning' app.log

# Print only names of files with a match
grep -l 'main' test-*.c

# Search the output of another command
ps aux | grep 'ssh'

The ps example searches the command’s output like any other input stream. Because it can also match the grep process itself, interpret its results accordingly.

Choose literal text or a regular expression

By default, grep uses basic regular expressions. Select a mode based on whether the pattern is plain text or has pattern syntax:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Option How it treats the pattern Example
-F Fixed strings; punctuation is literal, not regular-expression syntax. grep -F 'a.b' file.txt
-G Basic regular expressions; this is the default. grep 'error.*disk' app.log
-E Extended regular expressions, which offer convenient operators such as alternation. grep -E 'error|fail|panic' app.log
-P Perl-compatible regular expressions, if the installed GNU grep build has PCRE support. Check the local man grep before relying on it.

For example, grep -i 'hello.*world' menu.h main.c ignores case and matches lines with “hello” followed later on the same line by “world”; .* means zero or more characters. Basic and extended modes use different notation for similar matching tasks, while PCRE has distinct features and is conditional on build support.

# Match a whole word
grep -w 'root' /etc/passwd

# Exclude lines beginning with a comment marker
grep -v '^#' config.conf

Use -- to end option processing when an operand might begin with a hyphen, for example grep -F -- '-draft' file.txt. This prevents the text from being mistaken for an option.

Search directories recursively

Use -r to process files below a directory. In GNU grep, -r follows symlinks given as command-line operands but skips symlinks encountered during recursive traversal. GNU -R follows all symlinks, which can cause a search to cross into linked directories or revisit content.

# Search below project/
grep -r 'TODO' project/

# Follow all symbolic links (GNU grep)
grep -R 'TODO' project/

# Search only C and header files
grep -r --include='*.c' --include='*.h' 'malloc' src/

# Skip Git metadata and minified JavaScript
grep -r --exclude-dir='.git' --exclude='*.min.js' 'apiKey' .

GNU grep’s --include, --exclude, and --exclude-dir options use glob patterns to select files and directories during recursive searches. These long options are GNU extensions, so check availability before using them in scripts for other UNIX systems.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When file selection needs more control

For selection rules that grep’s recursive filters do not express, combine find with grep:

find /home/gigi -name '*.c' ! -type d -exec grep -H 'hello' '{}' +

The shell expands a pattern such as *.c before grep runs, whereas --include='*.c' is a glob used by GNU grep for recursive file selection. These filename globs are separate from grep’s regular-expression syntax.

Control what grep prints

These options change the output or how much input grep processes:

  • -n prefixes matching lines with their line numbers.
  • -l prints the names of files containing a match; -H and -h control filename prefixes.
  • -c counts matching lines in each input file.
  • -o prints only the matched text rather than the full matching line.
  • -m NUM stops reading a file after the specified number of matching lines.
  • -A NUM, -B NUM, and -C NUM print lines after, before, or around a match.
  • -q suppresses normal output for status-only checks.

Option behavior and available extensions can differ among GNU grep and other implementations. Consult the installed man grep when a command must work on a particular UNIX system.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use grep in scripts and portable commands

Grep’s exit status distinguishes a match, no match, and an error: a match returns success, no match returns a distinct nonzero status, and an error returns another nonzero status. This is useful for conditional checks, but scripts that need to distinguish all outcomes should verify the exact status behavior in the target implementation’s manual.

For portable commands across varied UNIX systems, prefer POSIX short options and test on the intended systems. -F is specified by POSIX and is the straightforward choice for literal text. GNU long options and -P are not safe assumptions for every grep implementation; PCRE support for -P also depends on how GNU grep was built.

Text-file behavior to know

GNU grep is designed for text and has no input-line length limit beyond available memory. If a file does not end with a newline, GNU grep supplies one for matching and output purposes.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.