The grep command
Print lines that match a pattern. Searches each file (or what is piped into it) and prints the lines that contain PATTERN. The pattern is a basic regular expression: plain words work as you would expect, . matches any character, ^ means the start of the line and $ the end. Quote patterns that contain spaces or special characters.
Usage
grep [OPTION]... PATTERN [FILE]...
Options
| -i, --ignore-case | match upper and lower case alike |
|---|---|
| -n, --line-number | show the line number of each match |
| -v, --invert-match | print the lines that do NOT match |
| -c, --count | print how many lines match, instead of the lines |
| -r, -R, --recursive | search every file in a directory and the folders inside it |
| -l, --files-with-matches | print only the names of files that have a match |
| -w, --word-regexp | match whole words only |
| -o, --only-matching | print only the matching part of each line |
| -E, --extended-regexp | extended regular expressions: + ? | and ( ) work without backslashes |
| -F, --fixed-strings | treat the pattern as plain text, not a regular expression |
| -h, --no-filename | don’t put the file name in front of matches |
Examples
grep error app.logLines containing "error"grep -in "error" app.logAny case, with line numbersgrep -r TODO projectsSearch every file under projectsls | grep .txtFilter another command’s output
Learn it step by step
Related commands
learner@sandbox: ~bash (practice)
Note: Try grep here: your home directory has sample files. Press Try beside an example, or type your own.
Enter runs · Tab completes names · ↑ ↓ step through history · Ctrl+L clears · type help for every command
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.