Recognize the Grep Command on Linux OS and How to Use it!

 

The grep command is a built-in Linux command for finding files based on a specific text. For Linux users, it's important to know how to use the grep command, as it can save time searching for and analyzing specific data.


Do you want to know more about grep and learn how to use this command? In this article, we will discuss the meaning of the grep command, how to use the grep command on Linux, as well as examples of the grep command for you to learn.


Immediately, here comes the full article!

What is Grep Command Linux?

The grep command is a basic Linux (UNIX) command to search for specific text within a file.

 Basically, grep stands for Global Regular Expression Print.


In other words, what grep does on Linux is to make it easier for you to find text files that contain certain strings of words, and see each line that contains those words.

It's very simple, isn't this grep command function? Even so, this Command-Line tool is widely used by System Administrators for various purposes.


For example, looking for malicious script snippets that are still left on UNIX systems after a backdoor attack. Or, it is also possible to reset the Daemon Service after practicing changing the SSH port on the VPS.


You already understand what the grep command is, right? Next, let's find out how to use the basic Linux grep command!

How to Use Grep Commands on Linux

Before starting to learn how to use grep Command on Linux, you need to prepare a device with a Linux operating system. As an example, we use the Niagahoster VPS service, which has Linux OS installed on it.


When ready, you can immediately open any terminal software installed on your computer. After that, please enter the VPS server by following the guide on how to use SSH.


To use grep, you simply write the grep command according to the syntax rules. After that, this tool will display the results in front of you.


So, here is the general grep command syntax:


grep [options] pattern [FILE]

The description is as follows:


grep – basic instructions or syntax for the grep command.

[options] – filter options to narrow search results.

pattern – the word or string to search for.

[FILE] – the name of the TXT file as a place to search.

There are many grep filter options you can use, such as grep -i, grep -c, grep -F, and so on. You can see it in full using the following command:


grep --help

The result is more or less like this:


But if it's still not clear, here are some of the most commonly used grep command options:

F – fixed string, displays lines of code containing certain words that are case sensitive.
i – ignore case, displays lines of code that contain certain words, but are not case sensitive.
w – word regexp, displays lines of code that contain certain words that form a whole word.
v – invert match, which displays all lines of code that do not contain a certain word.
n – line number, displays the line number of code that contains a certain word.
c – count, counts how many certain words are in a file.
r – recursive, searches for all files containing a certain word, complete with the path.
l – files with matches, searches for all files containing a certain word, without displaying the path.
So, that was the meaning of the grep command and how to use the grep command in the Linux terminal. Continue with the hands-on practice of the grep command example, come on!

5+ Examples of grep Commands for you to Try
The following are examples of using the grep command as well as the output it produces:

Search for words in a file
Search for words with case sensitive
Search for words without being case sensitive
Complete word search
Search for two or more words
Search for a word as well as a line of code
Counts the number of specific words in a file
Let's start with the first commandment!

1. Looking for Words in a File
The first example of the grep command is to search for specific words in a particular file. For example, if you want to find the word "lorem" in the contoh.txt file, the command to write in the terminal is:

grep lorem example.txt
The result is more or less like this:

the command grep lorem contoh.txt will highlight the word “lorem” in the file contoh.txt

2. Looking for Words with Case Sensitive
The next example of grep is to search for words in a case-sensitive manner (following the rules of case size). For example, if you want to find the word “Lorem”, grep will not return the words “lorem”, “LOREM”, or “LoREm”.

Here is the syntax for the grep command:

grep -F Lorem example.txt
Then the resulting output is:

the command grep -F Lorem contoh.txt will display the word "Lorem" taking into account the size of the letters contained in the file contoh.txt

3. Looking for Words without Case Sensitive
Contrary to the previous grep command, this syntax is used to search for words regardless of the case size. Here's an example:

grep -i lorem example.txt
Here are the results:

the command grep -i lorem contoh.txt will display the word lorem in the file contoh.txt regardless of the size of the letters or parts of other words

4. Looking for the Whole Word
The three grep commands above still output word fragments from other phrases. For example "lorem" from the word "dolorem". Well, if you just want to find a word that stands alone, then the command is:

grep -w lorem example.txt
The following output appears on the screen:


example command grep -w lorem example.txt will output the word “lorem” which stands alone, i.e. it is not part of another word

5. Search for Two or More Words
If you notice, the grep command example in the previous points can only be used to search for one word. So, how do you find two or more words in one command?

It's easy, use the command below:

grep 'lorem\|ipsum' example.txt
The command above will search the first word, then enter the pipe to find the second word. So, here's the output:

grep command 'lorem\|ipsum' contoh.txt will display the words "lorem" and "ipsum" contained in the file contoh.txt

6. Looking for Words as well as Lines of Code
You want to find a specific word, as well as which line number contains that word? If yes, you can use the grep command example below:

grep -n lorem example.txt
When executed, the display that appears on the screen is as follows:


the output of the grep command -n lorem example.txt is the word "lorem" as well as the line of code that contains the word

7. Counting the Number of Certain Words in the File
Lastly, an example of the grep command is to count how many specific words are contained in a file. For that, the grep command you need to type is:

grep -c lorem example.txt
Here is the result after executing:


example command grep -c lorem example.txt which will output how many words "lorem" is in the file contoh.txt

So, that was an example of using the grep command that you can practice. Don't move on yet, because we still have one important piece of info in the next point!

Grep Is A Must Try Command Line Tool!
Grep Command is a Linux tool that functions to search for groups of words in certain files. This makes grep widely used by System Administrators to maintain Linux OS performance, especially on VPS servers.

Even so, the performance of Linux VPS is not only affected by the use of the grep command. You need to pay attention to the quality of the service provider used, so that VPS performance is always excellent.


Post a Comment

0 Comments