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 herenothing runs on your computer
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
| Option | What it does |
|---|---|
-i | ignore upper/lower case |
-n | show line numbers |
-v | the lines that do NOT match |
-c | only count the matching lines |
-r | search every file in a folder, and the folders inside |
-l | only 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.