Do you want to find a file whose content is a specific text?

Doll with Magnifying Glass

Some have asked me about a functionality that the latest versions of Microsoft Windows have in which you can use their search engine to find, not only files with a certain name, but you can also include a text to find files that include such text, such as PDFs, Microsoft Office documents, .txt text files, etc. Well, you should know that in your GNU / Linux distribution you can do that and much more.

We have already done small tutorials in LxA of commands such as find, whereis, locate, etc. Now we are going to tell you a way to recreate this functionality that I have talked about in the first paragraph from your console. As I say Linux is extremely flexible and has various tools to find things, well here we are going to show you some of the different ways that you can use to search for files in which a certain text or string is found: To search for a word or string in the files of a directory you can use:

grep -s hola /home/*

grep -R hola /home/*

grep -Rw hola /home/*

In the previous example, we would look for the word "hello" within the / home directory in all the files that exist. In the first case, with the -s option a non-recursive search is performed, while with the -R it becomes recursive, so if there are subdirectories inside it will also search there ... But beware, that would search all the content with this string «hello ", Therefore if there is a phrase or word like" hello "it would also consider it valid and show the files that contain this, that is, it does not search for that word in a specific way. To make it specific you can use the third option.

Remember that it will do case-sensitive searches, so the previous examples would ignore things like Hello, HELLO, holA, etc. For me to make a search and ignore case, then you can use the -i option.

Imagine that you want to do a search in reverse, that is, all those files where do not include a string or word specific. Could you? The truth is that yes, for example:

grep -Rlv hola /home/*

grep --exclude-dir= /home/Desktop -Rlv /home/*

In the first example it will show the entire list of files that do not contain the word "hello", while in the second case it would do the same but files that are found are excluded hosted in / home / Desktop… By the way, this option –exclude-dir = can also be used in the first examples…


Leave a Comment

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

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  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.   James Vigo said

    Grazas per isto.