grep options, regex, parameters and regular expressions
The grep command allows you to search for a specific text string in a single or multiple file. This tutorial introduces this command and explains the options and patterns it supports to customize the text search and extraction process.
What is grep commnand?
The grep is a pre-installed command on Linux. It stands for Global Regular Expression Print. As the name suggests, it searches the specified regular expression globally in the provided source and prints the matching result.
- A regular expression is the text you want to search. It can be a single word or a complex text string called a pattern. The regex is the abbreviated form of the regular expression.
- A source can be a single file, the output of another command, multiple files, or a text buffer.
- It searches for regular expressions globally in the source. For example, if you provide multiple files as the source, it searches the pattern in all lines of all files and prints the result.
- It supports many options for formatting and customizing the output. For example, you can instruct it on what to include and exclude in the search result and how to format and display the finalized result.
- By default, it displays the result on the terminal screen. Using the shell redirection feature, you can save it in a file or redirect it to another command as the input.
The grep command syntax
The grep command uses the following syntax.
#grep [option] [pattern] [source]
Example
Create a text file containing a few names. Search a name using the grep command. Search for another name that includes space.

Enclosing a single word with single quotes is not required. However, enclosing the pattern with single quotes is necessary if the pattern contains space, multiple words, or meta characters.
The grep command options
Options are optional. They let you customize and format the search results. If you do not use them, it searches the pattern line by line in the given source. If it finds a match in any line, it prints that line at the terminal. The grep command supports many options. The following table lists essential options.
| Option | Description |
| -i | Ignore the case. When used, grep searches for the specified pattern in both (lower and upper) cases. Without it, the grep command searches the specified pattern only in the selected case. |
| -v | Exclude the specified pattern. If used, the grep command prints all the lines that do not contain the specified pattern. |
| -r | Search recursively. When used, the grep command searches the specified pattern in the specified directory and its sub-directories. |
| -A | Print additional lines after the matching string. |
| -B | Print additional lines before the matching string. |
| -o | Print only the matching contents. |
| -e | Use multiple regex. Use this option with each regex. |
| -E | Use the grep command in the extended mode. Used when the search pattern includes meta characters. |

Literal and meta characters
To specify a search pattern, we can use two types of characters: literal and meta.
A literal character is a regular character and has no special meaning. A character is literal when used for its actual meaning. For example, if you include the character "A" in the pattern and the grep command treats it as "A", then it is a literal character.
The following are the meta characters.
caret (^), dollar ($), input redirect (<), output redirect (>), period (.), asterisk (*), question mark (?), square brackets ([]), pipe (|), curly brackets ({}), parentheses (()), plus (+) and backslash ().
A meta character has two meanings: regular and special. You can instruct the grep command to use anyone as required. If you use it for the regular meaning, it will be a literal character. It will be a meta character if you use it for a special meaning.
There are two groups of meta characters: basic and extended. Basic meta characters are the following:
^ $ . [ ] *
Extended Meta characters are the following.
( ) { } ? + |
You can use the special meaning of basic meta characters without any option. To use the special meanings of extended meta characters, you must use them with the –E option. Unless you use them with the –E option, the grep command treats them as regular characters. For example, plus (+) is a regular character without the -E option, while it is an extended meta character with the -E option.
The following table lists the special meanings of meta characters.
| Meta character | Description |
| ^ (Caret) | Match regex at the start of the lines. |
| $ (Question) | Match regex at the end of the lines. |
| \ (Back Slash) | Turn off the special meaning of the following character. |
| [ ] (Brackets) | Match any one of the enclosed characters. |
| [-] | Define a range to match. |
| [^ ] | Match any characters except those mentioned in the [ ]. |
| | (Pipe) | Match both strings. |
| . (Period) | Match a single character for any value except the end of the line. |
| * (Asterisk) | Match zero or more of the preceding character or pattern. |
| {x,y} | Match x to y occurrences of the preceding. |
| {x} | Match exactly x occurrences of the preceding. |
| {x,} | Match x or more occurrences of the preceding. |
Search location
The grep command is incredibly versatile and capable of searching any source that contains text, including a single text file, multiple files, a single directory, multiple directories, the output of another command, an archive, a zip file, and more. Its flexibility makes it a valuable tool for a wide range of tasks.
This tutorial is part of the tutorial "The grep command in Linux: - usage, options, and syntax explained through examples.". Other parts of this tutorial are as follows:
Chapter 1 grep options, regex, parameters and regular expressions
Chapter 2 Grep Command in Linux Explained with Practical Examples
Chapter 3 Use Extended Regular Expressions with the grep command
Chapter 4 grep regex Practical Examples of Regular Expressions
Conclusion
This tutorial explained the grep command syntax and commonly used options. It also described the differences between literal and meta characters. Having this information enables you to use the grep command more effectively.By ComputerNetworkingNotes Updated on 2025-11-18