Basic Format
grep [path] [expression]Key Options
-i | Ignore case (for example, treat "abc" and "ABC" as equal) |
|
-v | Invert match (only show lines not matching pattern) |
|
-r | Recursive search |
|
-R | (same as -r) |
|
-l | Show only names of files containing the pattern |
|
-n | Show line numbers of lines containing the pattern |
|
-c | Count how many lines match the pattern |
|
-w | Match whole word |
|
-l | Show only the names of files with matching lines, separated by newline |
Examples
grep 'pattern' filename | Search for a pattern within a file |
|
grep 'string1|string2' filename | Search for lines containing string1 or string2 |
|
grep -r 'pattern' /dir | Recursive search for pattern in /dir |
|
grep -w 'full word' filename | Search for lines containing "full word" |
|
grep -n 'pattern' filename | Show line numbers of lines containing the pattern |
|
grep -c 'pattern' filename | Count the lines containing the pattern |
|
grep --color 'pattern' filename | Highlight the pattern in the output |
|
grep -i 'pattern' filename | Case-insensitive search |