There are times when you urgently need a particular file that contains important details. However, the sheer number of files and their locations can be confusing. That’s why Linux provides an extra layer of search capacity through the grep command. The grep command is a pattern-matching command in the Linux/Unix distributions.
The grep, a shortened form of “Globally Search for a Regular Expression and Print,” searches specific text inside your directories and files and prints with matching lines. In fact, it is one of the most basic Linux commands. So let’s start with the details about the grep command and then understand its operation with different examples.
What is Grep Command in Linux?
The grep command is available by default in many Linux distributions. However, if your system does not contain the grep, please use the following command:
sudo apt update sudo apt install grep (For Ubuntu/Debian) sudo yum install grep (For RHEL/Fedora)
Here is the basic syntax of grep command to use it in the terminal:
grep <option> <pattern> <file name>
How Use Grep Command in Linux?
Now let’s take a look at some examples of the grep command with their brief explanations:
1. Search Pattern in a Specific File
You can search for a specific word, pattern, or character in a file through the grep command. For example, let’s search “Linux” in a file named “file3” by the given below commands:
cd ~/<directory> grep <pattern> <filename>
As you can see in the image above, the word “Linux” is in “file3.”
2. Search Pattern in Multiple Files
You can search a specific pattern in various files using the grep command:
grep <pattern> <filename_1> <filename_2>.....
The above command displays all the files that include Linux. However, if the system doesn’t find a specific pattern or tag in the files, it automatically removes that particular file’s name from the output.
3. Search Pattern within a Directory
If you don’t want to type different files in the grep command to find a particular pattern, then you can use only a * to scan the complete directory:
grep <pattern> *
If you don’t know the exact pattern, please use the -i option to search for the pattern but with no same match restrictions.
grep -i <pattern> *
As you can see in both images, without the -i option, the system didn’t include file3 because it contains “unix” instead of “Unix.”
4. Inverted Search with Grep Command
With the grep command, you can search for those files that do not include the specific pattern. You only need to use the -v option with the grep command to discard occurrences of strings when debugging any problems in logs or files.
grep -v <pattern> *
The above command will show you a list of each file in the result that does not contain that specific pattern since this case-sensitive result has included small “unix” patterns.
You can remove the specific and used patterns from your result by adding the -i argument with the above command.
grep -vi <pattern> *
5. Search a Non-Alphanumeric Text Pattern
The grep command may break the syntax if you include a search string with other non-alphanumeric text and spaces. In this scenario, you can use the following command:
grep <"pattern"> *
After executing the command, the system displayed the file containing “Linux ?”
6. Search Subdirectories
If you want to perform a sub-directory-based search, please add the -r (recursive) option to the grep command.
grep -r <pattern>
7. Other Uses of the grep Command
We will discuss some additional uses of the grep command in Linux. Here is the list of different options available in the grep command:
Option |
Description |
-w option | Use this option for finding the whole words only. |
-l option | Use this option to only print the name of the file. |
-c option | Displays the name of the files and the number of lines where the specific pattern is available. |
-n option | Displays the number of files that have a particular pattern. |
-m option | Use this option to limit the output. For example, the below command displays only the first four files having the same pattern:
grep -m4 <pattern> * |
If you want to know more about the additional options available in the grep command, you can use the help option as shown below:
grep --help
Wrapping Up
The grep command in Linux/Unix offers a fantastic way to filter the files that contain a specific pattern. This command is excellent for finding particular files right from the terminal. That’s why we have included the complete details about the grep command and explained different examples.
The command is easy to use, but we recommend reading the guide thoroughly. Otherwise, you may face errors while using the grep command in your system. We hope the above information may help you use the grep command in Linux. Ask your questions below!
Aditya is a seasoned JavaScript developer with extensive experience in MEAN stack development. He also has solid knowledge of various programming tools and technologies, including .NET and Java. His hobbies include reading comics, playing games, and camping.