The grep command in Linux is handy for searching for text patterns within files. Grep stands for “global regular expression print,” and its main purpose is to allow a user to search and match text patterns within files specified in the expression.
The grep command is available in all Linux distributions, and no installation is required. Moreover, the command has numerous options that you can use to enhance your search. This post focuses on these options to help you become comfortable using them.
Grep Command Syntax and Options
The grep command in Linux follows the below syntax.
$ grep [options] [pattern] file
- The options represent the grep command flags you will add to modify your search.
- The pattern represents the regular expression for your search.
- The file represents the name of the file(s) to search within.
Below are some common grep command options you can use in your search.
Option | Description |
-i | Ignores the cases when searching within files. |
-r | Search through subdirectories. |
-c | Display a count of matched lines. |
-v | Display all lines that don’t match the pattern. |
-n | Show the matched lines and line numbers. |
-l | Only show the filenames matching the expression. |
-h | It only displays the matched lines, not the filenames. |
-o | Only show the matched part of the line. |
-f | Fetch the patterns from a specified file. |
-w | Match the whole word in the search. |
-q | Only confirm if there is a match without showing the results |
Grep Command Examples
So far, we’ve introduced the grep command in Linux, given its syntax and some of the commonly used options. The next task is to cover some practical examples of how to run the command with different options.
Basic Usage of the Grep Command in Linux
The primary way of running the grep command is with no options. For instance, we are searching for “linux” in the names.txt with the command below.
$ grep “linux” names.txt
The result will display all lines that match the specified pattern and highlight the matching pattern.
Ignore Cases in the Search
The -i option specifies that grep to search for the pattern within the file(s) and show results without considering the cases. Thus, if the pattern is “linux,” even “Linux” will be marched.
$ grep -i “linux” names.txt
Notice how the output below shows results where the search pattern is in mixed cases, unlike our first example, which was case-sensitive.
Search Multiple Files
There are two ways to approach this. Firstly, you can add all the names of the multiple files you want to search through.
$ grep “linux” [files]
Alternatively, you can use the asterisk (*) to search all the files in your current working directory.
$ grep “linux” *
Search Through Subdirectories
Another way to search for a pattern using the grep command in Linux is to include subdirectories. This option assumes that your search pattern could be in a subdirectory and not its parent.
We added the -r option and used the asterisk to search all the files and subdirectories.
$ grep -r “linux” *
We now get a matching output in the Records/file1.txt, which we would have missed with the standard search.
Show Count of Matched Lines
Suppose you are only interested in knowing how many matches have been found. Adding the -c option does the trick.
It will only show the count of matched lines without displaying the matches.
$ grep -c “linux” names.txt
We only have three matches for our example.
Grep Command in Linux Display All Non-matching Lines
Instead of focusing only on the lines matching the search pattern, you can focus on those that don’t match. To do so, add the -v option; the results will exclude all the matching lines.
$ grep -v “linux” Records/file1.txt
None of the lines below will match your pattern.
Include Line Numbers with the Grep Command in Linux
Sometimes, adding the line numbers for every matched line is handy, especially when you have a long file. Luckily, the -n will match every match with its line number for better results.
Such an instance is helpful when checking log files as you can quickly identify the pattern and access it in your log file using its line number.
$ grep -n “linux” names.txt
The line number will be displayed on the left of every matched line in your output.
Show Names of Matching Files
The grep command with the -l option will display the names of all the files that match your pattern. With this option, you won’t see the matched lines but will know which files contain your search pattern.
Here’s an example where we listed all files in our current directory that match our search pattern.
$ grep -l “linux” *
Search for the Entire Word within Files
Ideally, the grep command will match lines even if the search pattern is a substring. However, if you want to restrict the search to only display where the entire word is found and not as a substring, we will use the -w option.
$ grep -w “linux” names.txt
The example below shows the results of our grep command in Linux without the -w and what we get after adding the -w. Notice the difference in the matched lines.
Only Show the Matched Pattern in the Output
Although grep typically shows the entire line containing the matched pattern, it’s possible to restrict this output and only display the matched pattern.
The -o gets the trick done, like in the example below.
$ grep -o “linux” names.txt
Match Lines that Start with the Search Pattern
Still, on the grep command in Linux, you can set your search pattern to match only if it is found at the start of a line. It won’t be considered a match if it appears anywhere within the line.
Here’s how to implement this option.
$ grep -n “^linux” names.txt
We’ve also included the line numbers in our output.
Match Lines that End with the Search Pattern
Similar to how we’ve matched lines that start with the search string, we can do the same for lines ending with the search pattern.
Here, we will add our sign($) at the end of the search pattern, as demonstrated below.
$ grep -n “Linux$” names.txt
Source Search Patterns from a File
Instead of manually adding the search pattern when running the grep command, it’s possible to source the patterns line by line from a file.
Suppose we want to use the three search strings in our file below.
Read Also: How to use the cat command in Linux.
We could run our command with the -f flag, as shown below.
$ grep -f query.txt names.txt
Limit Output When Running the Grep Command in Linux
When you run the grep command, it will display all lines matching the search pattern.
However, you may have a case where you only want to view a specific number of outputs instead of all the matched lines.
In that case, add the -m with the number of lines to display. For instance, -m4 will limit the output to four lines.
$ grep -i -m4 “linux” names.txt
Utilizing Multiple Search Patterns
It’s possible to run the grep command with multiple search patterns. The output will display all lines where any of the search patterns match. Add the -e option before every search pattern.
Here’s an example.
$ grep -e ‘geeks’ -e ‘to’ -e ‘ls’ names.txt
Read Also: How to search for files using the find command in Linux.
Conclusion
The grep command in Linux is convenient for searching for patterns within files. You can utilize the grep command to achieve your search goal in numerous ways. This post has covered some of these ways and given practical examples to clarify doubts and get you comfortable working with the grep command.