grep¶
Search for patterns in files.
Synopsis¶
Description¶
Search for PATTERN in each FILE. If no files are specified or - is used, read from standard input. Matches are highlighted in color by default.
Flags¶
Pattern Matching¶
| Flag | Short | Description | Default |
|---|---|---|---|
--pattern-type |
-t |
Pattern type: basic, extended, fixed, perl |
extended |
--ignore-case |
-i |
Case-insensitive matching | false |
--invert-match |
-v |
Select non-matching lines | false |
--word-regexp |
-w |
Match only whole words | false |
--line-regexp |
-x |
Match only whole lines | false |
Output Control¶
| Flag | Short | Description | Default |
|---|---|---|---|
--line-number |
-n |
Print line numbers | false |
--with-filename |
-H |
Print filename with output | false |
--no-filename |
Suppress filename prefix | false |
|
--count |
-c |
Print only count of matches | false |
--files-with-match |
-l |
Print only names of files with matches | false |
--files-without-match |
-L |
Print only names of files without matches | false |
--only-matching |
-o |
Show only matched parts | false |
--quiet |
-q |
Suppress all output | false |
--ignore-binary |
Suppress output for binary files | false |
|
--max-count |
-m |
Stop after NUM matches per file | 0 |
Context Control¶
| Flag | Short | Description | Default |
|---|---|---|---|
--before-context |
-B |
Print NUM lines before match | 0 |
--after-context |
-A |
Print NUM lines after match | 0 |
--context |
-C |
Print NUM lines before and after | 0 |
File/Directory Handling¶
| Flag | Short | Description | Default |
|---|---|---|---|
--recursive |
-r |
Search directories recursively | false |
--include |
-I |
Search only files matching pattern | |
--exclude |
-e |
Skip files matching pattern | |
--exclude-dir |
Skip directories matching pattern | ||
--no-messages |
-s |
Suppress error messages | false |
Examples¶
Search for a pattern in a file:
Case-insensitive search:
Search with line numbers:
Recursive search in directory:
Show context around matches:
Count matches:
List files containing pattern:
Search for whole word:
Use fixed string (not regex):
Include only Go files:
Exclude test files: