Grep command: powerful tool to find text from Terminal

Grep command

Almost anyone who has a computer or who works with one will know the keyboard shortcut Ctrl + F to find text. The "F" stands for "Find", "to find" in English, and can be used, for example, to find text on a web page. This shortcut is also available in many applications, there are programs that use the "B" for "Search, but these shortcuts only work if we are inside the application and with the file open. In Linux we have very powerful tools that we can launch from the Terminal and if we want find any text that is in our team we will use the command grep.

grep It is a command that will help us to find text within the file that we indicate. Its name comes from g / re / p, a command that works for something similar in a Unix / Linux text editor. Like many other commands, grep Has many available options that we will add in the form of letters and each one will serve a different task. By combining these options we will be able to perform complex searches in one or more files. Here we show you everything you need to know.

with grep we will find any text in any file

First of all we will explain the available options:

  • -i: will not distinguish between upper and lower case.
  • -w: force it to only find specific words.
  • -v: selects the lines that do not match.
  • -n: shows the number of the line with the requested words.
  • -h: removes the prefix from the Unix file name in the output.
  • -r: searches directories recursively.
  • -R: like -r but follow all symlinks.
  • -l: shows only file names with selected lines.
  • -c- Shows only one count per file of the selected lines.
  • -Colour: Displays the matching patterns in colors.

In the image that you have heading this article I have looked for the word "Images" in the file "830.desktop" that is in that path. As you can see, I have written:

grep Imágenes /home/pablinux/Documentos/830.desktop

Keep in mind that in this article we will write examples that must be modified according to our search preferences. By saying "File", "Word", etc, we will be referring to the file with its path. If I had just written "grep Images 830.desktop" I would have received a message saying that the file does not exist. Or so it would be unless the file was in the root directory.

Other examples would be:

  • grep -i images /home/pablinux/Documentos/830.desktop, where "images" would be the word we want to find and the rest the file with its path. This example would search for "images" in the file "830.desktop" case insensitive.
  • grep -R images: it would search all the lines of a directory and all its subdirectories where the word "images" is found.
  • grep -c example test.txt: this would look for us and show the total number of times "example" appears in a file called "test.txt.

With grep we can also search for files

If what we want is to find the file 830.desktop, we will write the following command:

grep 830.desktop

This will perform a search for the file «830.desktop» in our personal folderIn other words, if the file is in the personal folder of another user, it will not be found. This is the most normal thing in the world because a user does not have permission to access the content of another without their password.

How to perform recursive searches

grep it also allows us perform searches subject to recurring rules or guidelines. For example, read all the files in each directory that contain the word "Pablinux". For this we will write:

grep -r Pablinux /home/

O well:

grep -R Pablinux /home/

We will see the results for "Pablinux" on a separate line preceded by the name of the file in which it was found. If we don't want to see the file names in the data output we'll use the -h option (from "hide"; hide):

grep -h -R Pablinux /home/

We can join the options together and write "-hR" without the quotes.

How to do exact word searches

Sometimes there are files that contain what we want to search for something else. For example, this can happen to us in compound words and by searching for "forests" we can find "rangers". If we want find an exact word we will use the -w option:

grep -w bosques /home/pablinux/Documentos/vacaciones.txt

The above command would search for "forests", ignoring rangers, in the file "vacation.txt" in the indicated path. If what we want is to search for two different words we will use the command gooseberry:

egrep -w bosques|plantas /ruta/del/archivo

Know how many times a word appears in a file

grep it is also capable of count how many times a word appears in a file. For this we will use the -c option:

grep -c prueba /ruta/al/archivo

Adding the option -n what we will see is the number of the line in which the word appears.

Reverse lookups

We can also do the opposite, that is, search for lines that do not contain a word. For this we will use the -v option, which would be as follows:

grep -v la ruta/al/archivo

The above command would display all lines that did not contain the word "the". This can come in handy in documents or lists in which a word is repeated many times and, for some reason, we need to access the rest of the lines.

Accessing system information with grep

PC model

grep not only is it capable of searching within files. It is also able to view system information. In the previous screenshot we can see how it shows us what model of PC we have (I know that it is not the most powerful laptop in the world). For this we have used the command:

cat /proc/cpuinfo | grep -i 'Model'

O well:

grep -i 'Model' /proc/cpuinfo

If what we want is to see the names of the disk units we will write:

dmesg | egrep '(s|h)d[a-z]'

How to List Only Matching File Names

If we want to see a list with only the names of the files that match a search we will use the -l option as shown below:

grep -l 'main' *.c

And if we want to see the word in colors we will write:

grep --color palabra /ruta/al/archivo

As you can see, the command grep is a very powerful tool It is worth it especially in cases where we do not remember where we have written something or in programming. In addition, it helps us to find out system information in a way that Terminal lovers will like. Has this guide on the command to find texts been useful to you? grep?


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   nordi said

    Hola!
    When you say that Grep is also used to search for files, I don't think it's correct because if you run grep EXPRESSION and don't pass the file to it, it will wait for the data input from the standard input.
    According to the manual page:
    grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or if the file “-” is given, grep searches standard input.
    That's why you can use it in pipelines, like the one that parses the output of / proc / cpuinfo by passing the output of this command as input to grep you can do the search.
    All the best

  2.   Ana said

    Very good post.
    You explain it very well and you go straight to the point.