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
| Flag | Description |
|---|---|
| -i | Case-insensitive matching |
| -r, -R | Search directories recursively |
| -n | Print line numbers |
| -l | Print only filenames with matches |
| -c | Print count of matching lines |
| -v | Invert match — show non-matching lines |
| -E | Use 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