Search

grep

Search for text patterns in files using regular expressions.

Synopsis

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

Examples

Search for 'error' in syslog
grep 'error' /var/log/syslog
Recursively search with line numbers
grep -rn 'TODO' src/
Case-insensitive search for warning or error
grep -i 'warning\|error' log.txt
List files containing the pattern
grep -rl 'import React' src/
Count matching lines
grep -c 'failed' auth.log

Common options

FlagDescription
-iCase-insensitive matching
-r, -RSearch directories recursively
-nPrint line numbers
-lPrint only filenames with matches
-cPrint count of matching lines
-vInvert match — show non-matching lines
-EUse extended regex (same as egrep)

About grep

The `grep` command search for text patterns in files using regular expressions. Search commands help you locate files, text patterns, and executables across the filesystem.

Efficient searching is critical when working with large codebases or troubleshooting system issues. These tools range from simple filename lookups to full regular-expression pattern matching across file contents.

The command accepts 7 commonly used flags shown above, though the full set of options is available in the man page (`man grep`). The 5 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More Search Commands

Other commands in the Search category

Related tools