grep command (Linux / bash / sh / zsh)
What it does
Search text for lines matching a pattern. When people search for the grep command linux, they usually want grep's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.
Search text for lines matching a pattern.
Syntax
grep [OPTION]... PATTERN [FILE]...
Common options
| Flag / option | Meaning |
|---|---|
| -i | Ignore letter case. |
| -r | Search directories recursively. |
| -n | Print matching line numbers. |
| -E | Use extended regular expressions. |
| -v | Select nonmatching lines. |
Practical examples
Search logs
grep -n 'ERROR' app.log
Shows error lines with line numbers.
Recursive search
grep -ri 'api_key' config/
Searches a tree without case sensitivity.
Exclude comments
grep -v '^#' settings.conf
Prints lines that do not begin with #.
Linux ↔ Windows equivalent
Closest match: Select-String (Windows / PowerShell). Search strings and files with .NET regular expressions.
Common mistakes
- Using a regular-expression metacharacter when a literal match was intended.
- Not quoting patterns containing spaces, glob characters, or `$`.
Related commands
- findWalk directory trees and select entries using tests and actions.
- sedTransform text streams with scripted substitutions and edits.
- awkProcess record-oriented text with patterns, fields, and actions.
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- catConcatenate files and write their contents to standard output.
- Get-ContentRead file content or provider data as a stream of objects.
- echoWrite arguments and shell-expanded values to standard output.
- echoDisplay text or control command echoing in a batch script.
FAQ
What does grep do in Linux?
Search text for lines matching a pattern.
How do I use grep from the terminal?
Start with `grep [OPTION]... PATTERN [FILE]...`, then choose only the options needed for the task.
What is a practical grep example?
Try `grep -n 'ERROR' app.log` — Shows error lines with line numbers.
Can I use grep in a shell script?
Yes. grep works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.