Search

Linux grep command

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

Availability: Common Linux commands; shell built-ins and external implementations may differ by distribution.中文

Syntax

grep [OPTION]... PATTERN [FILE...]

Examples

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

Parameters, options and clauses

Option / clauseDescription
-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.
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory