grep command in Linux: search text by pattern
Updated November 1, 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 outputSample output
$ grep ERROR app.log
2026-07-19 09:16:10 ERROR connection refused to redis:6379
2026-07-19 09:16:16 ERROR connection refused to redis:6379
2026-07-19 09:22:13 ERROR unhandled exception in worker 3
$ grep -rn ERROR srv
srv/conf/app.conf:1:level = ERROR
srv/app.log:7:2026-07-19 09:16:10 ERROR connection refused to redis:6379
srv/app.log:9:2026-07-19 09:16:16 ERROR connection refused to redis:6379
srv/app.log:13:2026-07-19 09:22:13 ERROR unhandled exception in worker 3How 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, usegrep -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.
Common mistakes
grep ERROR with no file name is not an error. grep reads standard input and waits in silence for you to type, which looks like a frozen terminal. Ctrl+D (end of input) or Ctrl+C gets you out. Give it a file, or feed it text through a pipe.
A pattern with a space has to be quoted. Without quotes grep connection refused app.log reads differently: the pattern is connection, while refused and app.log are file names. Back comes grep: refused: No such file or directory, and the matching lines now carry an app.log: prefix because there are two files. Write grep 'connection refused' app.log.
ps aux | grep nginx always finds itself: the process list contains the grep nginx line. One matching line does not mean the service runs. Drop the noise with ps aux | grep nginx | grep -v grep, or ask pgrep -a nginx instead.