Linux is a develop(ment)er-friendly operating system. There are several commands that it supports, among which some are essential. One such is the grep command in Linux.
To cut the chase, the Linux grep command is a very important command that is basic yet powerful. If you wish to develop using Linux then you should definitely know about it.
What is the grep Command and what it can do for you?
What is the grep meaning? The GREP stands for Global Expression Regular Print, which is used in Linux/Unix terminals as grep. This command allows you to search and match text (strings) in a file [Linux search text in files].
The grep Linux is among the most common, easy-to-use, and powerful commands that you should know if you are a Linux/Unix system user. Additionally, if you are a developer, you should know the grep usages and their scope.
The name of grep signifies what it does. It initiates:
- a global search for
- a regular expression, and
- Prints the matching line or lines as a terminal output.
Primary Benefits of Using the grep Command
Before moving forward to observe some examples that demonstrate how the grep command in Unix works in practice, take a look at some of the benefits of using it:
- It is possible to locate and identify the files faster that contains the text (string) that you want to search.
- If you are a developer, the grep command can give you an upper hand while debugging the code.
- You can also find empty folders and blank files quite easily in your Linux-based system.
How to Use GREP Command in UNIX/Linux Terminals
It’s quite easy to make use of the grep command in your Linux- or UNIX-based system. As you might already have an idea that for using this command, you need to open the terminal on your system.
Now to use the grep command, the first thing you need to do is to understand the grep syntax, which is as follows:
grep [OPTION] PATTERNS [FILE]
- OPTIONS - Defines the way the grep will behave while searching for a text or string.
- PATTERNS - The text pattern or the regular expressions that need to be searched.
- FILE - It specifies one or more files in which the pattern will be searched.
A simple instance of the grep command in action would be:
grep -i “Linux” sampleTextFile.txt
This command basically means that grep will need to find the word Linux in the file named sampleTextFile.txt. And the -i flag, which is a grep option. This means that the search term is case insensitive.
In the output, you will be able to see all the lines that have the word Linux in them, just keep a note that no consideration will be given to the casing due to the -i flag.
Some Grep Examples of using in Linux
To develop a better understanding of the Grep command, here are some examples that you should take a look at:
1. Get the name of files that contains a specific pattern (string)
Assume that you want to know which files have the pattern or word “Linux” in it. So, what you need to do is to run the following command in the terminal:
grep -l “Linux” *
At the output, you will be able to see the name of the file that contains the pattern:
2. Get the number of matches for a pattern in a file
Now, you may need to find the number of matches for the word “Linux” in the same file i.e. sampleTextFile.txt. To do that, you will have to use the following command:
grep –c “Linux” sampleTextFile.txt
In the output, you will get the number of exact matches of the pattern found in the file, which is 2 in this case:
3. Get the exact matched pattern in a file
If you only want to get only the matched pattern within a file, you need to pass the -o flag in the grep command. For instance, consider that you need to search for the pattern “Linux” in the file sampleTextFile.txt, then you would have to use the command below:
grep -o “Linux” sampleTextFile.txt
At the output, you will only get the exact pattern, i.e. “Linux”.
4. Get Full Words Containing the Searched Pattern
Consider that you want to search all the words having the pattern “UX” in them in the sampleTextFile.txt file. For that you need to run the following grep command:
grep ux sampleTextFile.txt
At the output, you will see the word Linux highlighted at 3 different locations as it has the pattern UX in it:
5. View the number of lines containing the pattern
If you want to see the number of the lines in which the pattern is present, you need to use the flag -n as the option in the grep command.
Consider that you want to view the line numbers of lines containing the pattern “Linux” in the sampleTextFile.txt. In that case, you need to run the following command in the terminal:
grep -n “Linux” sampleTextFile.txt
The result would look like this:
6. Searching for Multiple Patterns
It is also possible to search for multiple patterns using a single grep command. Suppose you want to search for two patterns: “Linux” and “open-source” altogether in the sampleTextFile.txt file, then you need to run the following command:
grep ‘Linux \| open-source’ sampleTextFile.txt
At the output, you will see both the patterns highlighted within the text:
7. Get the file name containing the pattern along with the file path
Suppose you want to know both the name of the grep file and their respective locations in the system that has the pattern, let’s say “Linux”. In that case, you would have to run the following command:
grep -rwn ./ -e “Linux”
Here’s what the output will be:
Notice that the grep command will also locate the pattern in the bash history. It contains the history of the commands that you ran on the terminal.
Note: The examples above are basic representations of how you can use the grep command. The scope of the bash grep command, however, is quite wide in practicality. As such, there are several advanced ways to use grep for searching patterns within files located on your Linux or UNIX systems.
Wrapping it up!
That sums up our take on the grep command in Linux. You will not be able to appreciate and work with it. Remember, it is a very helpful command that any Linux user, irrespective of the skill level, must know how to take advantage of.
Is there something wrong written above? Let us know so that we can mend it ASAP and make this write-up better. Want to boast some creative/advanced ways of using the grep command? Use the box below!
People also read: