Forever & AlwaysLINUX COMMAND ATLAS

LINUX COMMAND ATLAS · COMMAND GUIDE

Linux grep command

Search text for a pattern. Use -R for directories and -n to show line numbers.

GNU Grep

Syntax

Command form
grep [OPTION]... PATTERN [FILE...]

Common options

8 options
OptionWhat it does
-i, --ignore-caseIgnore case distinctions.
-n, --line-numberPrint line numbers with matching lines.
-r, --recursiveSearch directories recursively (follow symlinks to files).
-v, --invert-matchSelect lines that do not match.
-E, --extended-regexpInterpret PATTERN as an extended regular expression.
-F, --fixed-stringsTreat PATTERN as a list of literal strings.
-w, --word-regexpMatch whole words.
-l, --files-with-matchesPrint only names of files with matches.

Usage examples

3 examples

Search text for a pattern. Use -R for directories and -n to show line numbers.

grep -Rni "error" ./logs

Show matching lines and their line numbers.

grep -n 'error' app.log

Search .conf files recursively, ignoring case and showing line numbers.

grep -Rni --include='*.conf' 'timeout' ./config

Notes

Quote patterns containing shell characters. Use -F for literal text and -E when regular-expression syntax is intended.