Linux Command grep plays a crucial role in quickly locating the necessary information when dealing with numerous files and text data during your work. In this post, we will explore the basic usage of the grep command and various options available.
Table of Contents
What is Linux Command grep?
The term grep stands for “global regular expression print,” and it is a command used to search and print lines that match a specific pattern in text data. It is particularly useful when you need to search for a word or a string in the contents of a file or in text output.
Basic Usage
The basic format of the grep command is as follows:
grep [options] search_pattern filenameShellScriptThe content of the example.txt file, which we will use in the upcoming examples, is as follows:
Hello, how are you doing today?
It's a pleasure to meet you.
Nice to meet you as well.
HELLO, this is Charles speaking.
Can I assist you with anything?
Do you need any help?
hello, may I ask you a question?
Could you please help me with this?
hello, I'm calling regarding the job posting.
I'd like to make a reservation.PlaintextLet’s look at the most basic example. For instance, if you want to search for the word “hello” in the example.txt file, you would type:
grep "hello" example.txtShellScriptThis command will output all the lines in the example.txt file that contain the word “hello.”

Here’s the result when searching for lines that contain “this”:

Key Options
The grep command offers a variety of options that allow you to fine-tune your search results. Here, we will introduce some of the most important options.
-i (ignore case): Search Without Case Sensitivity
By default, grep is case-sensitive. However, by using the -i option, you can search without distinguishing between uppercase and lowercase letters.
grep -i "hello" example.txtShellScriptThis command will output all lines containing “hello,” regardless of whether it’s “Hello,” “HELLO,” or any other case combination.

-v (invert match): Output Lines That Do Not Match
The -v option outputs lines that do not match the search pattern.
grep -v "hello" example.txtShellScriptThis command will output all lines that do not contain “hello.” Below is the output when using the -i option together to print lines that do not contain “hello,” regardless of case.

-r (recursive): Search Files Within a Directory
The -r option allows you to search through all files in a specified directory and its subdirectories.
grep -r "hello" /path/to/directoryShellScriptThis command will search for “hello” in all files within the /path/to/directory directory. The figure below shows the output of lines containing “hello” from files within the current directory and its subdirectories. The filename is displayed at the beginning so that you know which file the content comes from.

-l (files with matches): Output Only Filenames
The -l option outputs only the names of files that contain the search pattern.
grep -l "hello" *.txtShellScriptThis command will output the names of all .txt files in the current directory that contain “hello.”

-n (line number): Output Line Numbers with Matching Lines
Using the -n option, you can display the line numbers along with the lines that match the search pattern.
grep -n "hello" example.txtShellScriptThis command will output the line numbers along with the lines containing “hello.”

-c (count): Output the Number of Matching Lines
The -c option outputs the number of lines that match the pattern.
grep -c "hello" example.txtShellScriptThis command will output the number of lines in the example.txt file that contain “hello.”

-A (after context), -B (before context), -C (context): Output Context Lines
The -A option outputs N lines after the matching line, the -B option outputs N lines before the matching line, and the -C option outputs N lines both before and after the matching line.
grep -A 2 "hello" example.txtShellScriptThis command will output the line containing “hello” and the two lines following it. If there aren’t two lines following the match, it will only display as many lines as are available, as shown below.

Next, the -B option is used to output the line containing “hello” and the two lines preceding it.

Finally, using the -C option will output the line containing “hello,” along with the two lines before and after it.

Practical Use Cases
Analyzing Log Files
It is useful for searching specific error messages in system log files. For example, to find “error” messages in the /var/log/syslog file, you would type:
grep "error" /var/log/syslogShellScriptSearching Source Code
You can use grep to search for specific function calls within source code files. For example, to search for the main function in all .c files in the current directory, you would type:
grep -n "main" *.cShellScriptSearching File Contents Within a Directory
This is useful for searching through all files in a specific directory. For example, to search for the word “network” in all files within the /etc directory, you would type:
grep -r "network" /etcShellScriptSummary
In this post, we explored the basic usage of the grep command and various options available. Grep is a powerful tool for efficiently searching through files and text data, and it can be applied in various scenarios such as log file analysis, source code searching, and searching file contents within a directory. By utilizing these options effectively, you can achieve optimal search results tailored to your needs. Mastering grep will significantly enhance your efficiency when working in Linux.