A Guide About the Grep Command to Search Files in Linux
The Linux terminal is a quite powerful utility with numerous useful commands to control your operating system. The global regular expression print command or GREP is among the ones Linux users frequently utilize.
You can use Grep to search multiple files for lines that will provide a match to the set pattern. If you are interested in learning how this command works and how to find Grep exact match, take a look at this tutorial.
Here is how the syntax of the Grep command should look:
grep [functions] pattern [file_path1]
Now, let’s take a look at how we can use the above syntax to search for a specified text in a particular file.
grep Mark /folder/file.txt
The system will now look for any lines containing “Mark” in the “folder/file.txt.” It will display one line at a time in the output.
If you want to look for multiple words that include spaces, your strings need to look like this:
grep "Mark Stevenson" /folder/file.txt
Quotation marks are a must whenever you are searching for text (or string) with spaces.
If you only want to find a file or a folder you can use the find command.
Grep Exclude (Invert Match)
You can also turn the search upside down. Instead of looking whether a file contains lines with a particular pattern, you will ask the system to display all lines that do not match that string.
Here is how that looks:
grep -v "Mark Stevenson" /folder/file.txt
The “-v” function is nothing else than the command to execute an inverted search. The system will now display all lines in the “folder/file.txt” file that do not contain the string “Mark Stevenson.”
Search a Command Output with Grep
An extremely useful option that the Grep command offers is to look for a pattern in the result of another command. The idea is simple – you start by issuing the other command and then follow it with the conditions of the Grep search.
Here is an example:
ps -ef | grep username
The above line first told a Linux-based OS to look for system processes, but Grep specified to look only for processes run as a particular “username” account.
You can further customize the results by utilizing the invert match function. For example let’s say that you want to display all results, but you want to exclude the particular grep process to be shown.
Here is the line that you should use:
ps -ef | grep username | grep -v grep
The system will now display all processes that match the particular username except the grep one.
How to Use Grep to Do a Recursive Search
The next option we will analyze is the recursive function. You can use it by adding “-r” before the desired pattern to search.
Let’s see how the syntax can look:
grep -r "Mark Stevenson" /etc
As you can see, the recursive search is marked with the “-r” option. A lowercase letter indicates that the system should skip symbolic links. If you want to include them in the search, you can use the “-R” with the uppercase letter.
In the above command, we also modified where the system should search for the patter “Mark Stevenson.” Instead of a single file, we told the machine to look in the “/etc” folder, which means it will search all files in that particular folder.
Apart from the result, the output will also show the exact path to the file where the pattern appears.
How to Modify the Output to Show Only Filenames
If you do not need extensive information about the results, and you only want to discover the files where the pattern exists, use the following:
grep -l "Mark Stevenson" *.txt
The above line will look for “Mark Stevenson” in all .txt files in the particular folder. However, the “-l” option will tell the system only to display the names of the files with matches.
You can also consider combining this with the recursive search. In that case, you should use the “-Rl” option.
How to Make a Case Insensitive Search with Grep
It is important to note that the results of the search when using the grep command will depend on the case sensitivity. However, there is an option that tells the system to do a case-insensitive search.
Here is how the command looks:
grep -i "mark Stevenson" /etc
Regardless of lower can uppercase letters used in the pattern, the system will search for both and display all files in the “/etc” folder where it found the “mark Stevenson” string.
The option to ignore the case can be useful in case there are typos within the files you are searching. For example, if you accidentally used the wrong case when creating the file, such as writing “mArK StEvenSon,” this search will have you covered and display the name among the results despite the case mistakes.
Grep Exact Match
When looking for a single-word pattern, the grep command will show all results where that word is separate, but also results where it is a part of other words.
For example, if you search for “Mark,” the results may include:
Mark Markus Mark’s Marks Markings Markers
This is where the grep exact match can come in handy. If you use the “w” option, you can search only for files where the pattern is shown as a whole word.
Here is an example:
grep -w Mark /folder/file.txt
The system will now display only those lines where “Mark” is found as a separate word. Please note that the search will include cases when your searched pattern is enclosed by non-word characters. These are all characters that are not alphanumeric or underscores.
How to Show Line Numbers
Another useful option when using the grep command is the “-n” option, which shows line numbers where the specified pattern was found.
Here is how the command looks:
grep -n Mark /folder/file.txt
The search output will start with a line number now.
You may also want to know how many files have that particular string. That is something you can find out by utilizing the “-c” function.
Here is the command:
grep -c Mark /folder/file.txt
The system will now display the number of results matching “Mark” in the specified file.
Can You Search for Multiple Patterns?
You have an option of searching for more than one pattern when using the grep command. Here is how you can do that:
grep "Mark\|Stevenson\|Bartender" /folder/file.txt
The above line will display all the lines that match at least one of the words Mark, Stevenson or bartender.
What You Should Know about Grep Regular Expressions
There are two grep regular expressions:
If you choose the basic expression, the characters are considered regular expressions in the pattern that match themselves.
The only exceptions are metacharacters, and here is how you can use some of them:
- ^– this symbol indicates that the system should only look for the pattern at the beginning of a line. For example, “grep “^Mark” document.doc” will only display results where Mark is at the beginning of the line.
- $– similar as above, except this tells the system to look at the end of the lines. The syntax looks like this: “grep “$Mark” document.doc” and tells the machine to look at the end of every line.
- .– think of using the period as a wild symbol. The system will search for any character at any place where you place a dot. For example: “grep “M. .k” document.doc” will search for all four-letter words (because of the two dots used) starting with M and ending with k. Those can include Monk, Mark, Munk, etc.
-  – if you do not want the system to search for any characters, but only particular ones, you can take advantage of brackets. For example: “grep “Ma[rs]k” document.doc” will display all lines where “Mark” or “Mask” is identified.
- [^] – if you want to exclude a particular letter from the brackets, here is what you can do: “grep “Ma[^r]k” document.doc” The previous command will exclude “Mark” from the search, but look for other four-letter combinations with different third letter, such as “Mask” and others.
You can combine the meta-characters above to suit your needs. If you do not want to utilize the special meaning of the characters mentioned above, you can use “\” before typing them.
You can also use extended regular expressions when using the grep command. These are activated by using the “-E” function. These will add extra meta-characters to modify the search further.
The extended expressions are considered advanced, so you should make sure to practice using grep before resorting to using them.
We will show you an interesting example of how to use extended regular expressions and the grep command to tell the system to extract all e-mail addresses that match a particular pattern in the specified file:
grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.com" emails.doc
The above line will look for all .com e-mails in the “emails.doc” file.
As you can see, there are plenty of things that you can do with the grep command. Whether it is searching for a grep exact match, or excluding particular patterns from the search, you can customize the search to your needs as long as you are a bit creative.
If you are a fan of using Python programming language, you can make your life easier by using Python Pip. It is a package management utility that allows you to install and manage Python software packages easily. Ubuntu doesn’t come with pre-installed Pip, but here…
If you are looking for a convenient way to manage Java applications on Ubuntu, the solution may lie in the IntelliJ IDEA development platform. An excellent advantage of this option is that it is relatively suitable for beginners, but experienced users will also find it…