Title: Getting to Know the Grep Command on Linux OS and How to Use It
For You. Get More Information from our site @iwanrj.com free.
grep command is a built-in Linux command to find files based on 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 grep command meaning, how to use grep command on Linux, as well grep command examples for you to learn.
What is Grep Command Linux?
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, the function of grep 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 how to change the SSH port on the VPS.
How to Use Grep Commands
The grep command or grep command is part of the Unix operating system and is known as one of the essential multi-purpose tools. The function of this tool is to search for a pattern in a text file that has been created. In other words, using the grep command makes it easier for you to search for words or patterns and one or more lines which will then be displayed.
At first glance, gerp looks like a command with minimal functionality and usability. However, for sysadmins who handle various services with various configuration files, the gerp command is used to send requests or search for certain lines in the file.
Before starting to apply the gerp command, first make a connection to the VPS using SSH. Check this article for more information on how to login to VPS with PuTTY SSH.
ssh [email protected]
If the operating system used on the computer is Linux, then you only need to open a terminal.
The following grep command syntax to search for a file:
grep [options] pattern [FILE]
- grep – grep command basic instructions or syntax.
- [options] – filter option to narrow search results.
- patterns – the word or string you want to search for.
- [FILE] – TXT file name as a place to perform the search.
To read guides and explanations of the various options offered, type the following command at the command line:
grep --help
As you can see, there are various options offered by the grep command. However, the most important and commonly used options are:
- F – fixed strings, displays lines of code that contain certain words with case sensitive.
- i – ignore case, displays the line of code that contains the specified word, but is not case sensitive.
- w – word regexp, displays lines of code that contain certain words that form a whole word.
- v – invert match, displays all lines of code that do not contain a specified word.
- n – line number, displays the line number of code containing the specified word.
- c – count, counts how many certain words are in a file.
- r – recursive, search for all files containing a certain word, complete with the path.
- l – files with matches, searches all files containing the specified word, without displaying the path.
Examples of grep Commands for you to Try
The following are examples of using the grep command as well as the output it produces:
1. Search for a word in a file
The first example of the grep command is to search for specific words in a particular file. For example, you want to search for words “lorem” on files example.txt, So the command to write in the terminal is:
grep lorem contoh.txt
The result is something like this:
the command grep lorem contoh.txt will highlight the word “lorem” in the file contoh.txt
Tabel:
2. Search 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, you want to find the word “Lorem”, then grep will not show the word “lorem”, “LOREM”, or “LoREm”.
Here is the grep command syntax:
grep
-F
Lorem contoh.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. Search for words without being 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 contoh.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. Look for the word as a whole
The three grep commands above still output word fragments from other phrases. For example “lorem” from word “dolorem”. Well, if you just want to find a word that stands alone, then the command is:
grep
-w
lorem contoh.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:
|
The command above will search for the first word first, 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 contoh.txt file
6. Look for the word as well as the line of code
You want to find a specific word, as well as which line number contains that word? If so, you can use the grep command example below:
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. Count the number of specific words in the file
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:
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