Linuxlab

grep command in Linux: search text by pattern

Updated July 3, 2026

The grep command searches text for lines that match a pattern and prints them.

Syntax

grep [options] pattern [file...]

It can read a file or take text from a pipeline with |.

Common cases

grep error app.log        # lines containing error
grep -i error app.log     # ignore case
grep -n error app.log     # with line numbers
grep -r error .           # recursively through a directory
grep -v debug app.log     # every line EXCEPT debug
ps aux | grep nginx       # filter another command's output

How it works: what the name grep means

The name stands for global regular expression print: walk every line, match it against a regular expression, and print the matches. By default the pattern is a regular expression, so ., * and [] have special meaning. To search for a literal string with no magic, use grep -F.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand. The container is deleted when you leave, so there is nothing to be afraid of breaking.

Where this is in the book: the chapter Streams, redirection, pipelines.