grep

Find the lines that contain a word, in one file, a whole folder or the output of another command.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • grep express server.js
  • grep -n listen server.js
  • grep -i EXPRESS server.js
  • grep -v require server.js
  • grep -rl express .
  • ps aux | grep nginx

How it works

grep prints every line that contains a pattern. Give it the word and one or more files, or a folder with -r.

It shines in pipes: ps aux | grep nginx filters the output of another command down to what you care about.

Syntax

grep [OPTION]... PATTERN [FILE]...

Options

OptionWhat it does
-iignore upper/lower case
-nshow line numbers
-vthe lines that do NOT match
-conly count the matching lines
-rsearch every file in a folder, and the folders inside
-lonly the names of the files that match

Good to know

  • Quote a pattern with spaces: grep "port 3000" server.js.
  • No output and no error means no line matched.