How to use the Grep command in Linux

If you work with this operating system, then you should learn How to use the Grep command in Linux. This is a command that was created to help people find more accurate information after running a command, so searches are a little faster.

How to use the Grep command in Linux?

To learn how to use this command to search for any pattern, either in some file or a group of these, The syntax that must be used is the following:

  • grep ' ' <file/files>

You must bear in mind that single or double quotes will be required in the text if it is more than one word. Besides, you could use the wildcard (*) so you can select all the files in a same directory.

This will return the occurrences of the pattern according to the line in which it is found in the file or group of files. If you notice that there is no match, no output will be printed in the terminal where you are working.

Example

Next, we are going to give you an example so you can learn how to use the Grep command in Linux:

Let's assume that we have the following files named grep.txt:

  • Hello, how are you
  • I am grep
  • Fine thank you and you 

The following grep command will have as function, searching for any occurrence of the word you.

  • Grep you grep.txt

This will result Hello, how are you, Fine thank you and you.

If you work with this command, what is expected is that the word you be seen in a different color than the rest of the text, so you can easily identify what you're looking for. Grep comes with many other options that can help you achieve much more when searching.

What are the options to improve the search?

If you want to learn how to use the Grep command in Linux to have better searches, you should keep in mind that there are many options for it, but this time we are going to show you two so that you have an idea about it and these are the following:

  1. -n (–line-number) – list line numbers: You can print all the matches that exist in a text, next to the numbers of each of the lines. If you pay attention to the search result in the previous point, you will be able to notice that there are no numbers in the lines, only some matches.

Grep you grep.txt -n. The result of this will be that the following will appear:

  • Hello, how are you
  • Fine thank you and you
  1. -c (–count) – print the number of lines of matches

Grep you grep.txt -c. The result of this will be a 2.

You should note that in case there is another you on the first line, the -c option can still print 2. This is because is related to the number of lines in which matches are found, it would not be the number of these.

Searching for files with the Grep command

If your idea is to find the file named 830.desktop, you should write the command that appears below:

  • Grep 830.desktop

Running this command will start searching for the file 830.desktop which is located in the personal folder that is your user. This means that if this file is located in the personal folder of another user, it will not be found, this is very normal because if a user does not have the permission to enter another person's content without having the key of this.

recursive searches

You can learn how to use the Grep command in Linux for this type of search. If you want to search for files that have the word Opening, you must write either of the two options below:

  • Grep -r Open /home/
  • Grep -R Open /home/

There you will see all the search results for the word Opening on a separate line which will be next to the name of the file where this word was found. If you don't want to see the filenames right on the output, we're going to use the -h “Hide” option; hide:

  • Grep -h -R Open /home/

Options can join and you can also write hR without using quotes.

Reverse lookups

To finish this article, we recommend that you learn about reverse searches, these are those that do not contain a single word. To do this, you must use the -v option and this is as follows:

  • Grep -v the path/of/the/file

The above command will show you the lines that do not have the word "the". This could be found in lists or documents in which it is not repeated as much.

Leave a comment