How to find all files containing search text on linux?

Question:

I would like to find a way to find all files that contain a given text. It is the text inside the files, not in the names.

There is such a solution:

find / -type f -exec grep -H 'текстДляПоиска' {} \;

However, it works quite crookedly, how to do it correctly?


Free translation of the question: "How do I find all files containing specific text on Linux?"

Answer:

Do this:

grep -rnw '/путь/к/папке/с/файлами' -e "шаблон"
  • -r or -R – recursive search.
  • -n – displays the line number.
  • -w – whole words only.
  • -l (lower case from L ) – Output the filename where there was a match.

The following flags will add efficiency:

  • --exclude – Pattern to exclude files, for example: search everywhere except for files with the extension .o :

     grep --exclude=*.o -rnw '/путь/к/папке/с/файлами' -e "шаблон"
  • --include – Search only in certain files, for example: only in files with the .h and .c extensions:

     grep --include=\*.{c,h} -rnw '/путь/к/папке/с/файлами' -e "шаблон"
  • --exclude-dir and --include-dir – the same, only for selecting directories, for example: exclude folders один , two and any starting with а :

     grep --exclude-dir={один,two,а*} -rnw '/путь/к/папке/с/файлами' -e "шаблон"

This works great for similar purposes, the rest of the settings for grep can be found by running man grep .


When forming the answer, the answer to the question "How do I find all files containing specific text on Linux?" from contributor @rakib .

Scroll to Top