Skip to main content

Command referencegrep

grep command (Linux / bash / sh / zsh)

Linuxbashshzshtextbeginner

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 / optionMeaning
-iIgnore letter case.
-rSearch directories recursively.
-nPrint matching line numbers.
-EUse extended regular expressions.
-vSelect 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 `$`.

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.