Search for all files that contain a text string in Linux


Original question: Finding all files containing a text string on Linux from Nathan

I'm trying to find a way to scan my entire Linux system for files with a specific text string. Just to be clear, I look for a text string in the file, not the file name.

When I was looking for how to do it, I found this solution twice:

find / -type f -exec grep -H 'cadena-de-texto-para-buscar' {} \;

However, it did not work. Apparently, it shows all the files in the system.

Is this the best way to do it? What should it be? This ability to search for strings of texts within archives would be extraordinarily useful for some programming projects that I am doing.

asked by Guilherme Bussi Dias 21.12.2015 в 22:18

7 answers


grep -Ril "texto" * is my favorite way to do this search.

However, it is also useful to use find to get the list of files with the necessary filters and then execute grep to see which ones contain the string we are looking for:

find -type f -exec grep -l "texto" {} +

This searches for files only and in all of them searches for the text "text". If there is a match, it automatically comes out thanks to the use of -l (L lowercase), so the output of this command is the list of files containing that string.

answered by 22.12.2015 в 13:36

Use this:

grep -Ril "texto-a-encontrar-aqui" /

R is to be recursive, i is to ignore the case and l to display the names of the files.

answered by 22.12.2015 в 02:34

I think this should work for you:

Find has a very simple syntax just need to write

find [ruta] [expresión_de_búsqueda] [acción]
  • [route] any path from where the search starts, for example, /home/usuario .
  • [search_expression] default -print shows the search result.
  • [action] Linux command to run on each directory or file found



All the files belonging to Guillermo

find /home -user guillermo

Find files with the word report in the name.

find /home -name informe    

Do the same search ignoring case.

find /home -iname informe   

files that start with a number between 0 and 9

find /home -name "[0-9]*"   

files that start with "A" or "g".

find /home -name "[Ag]*" 

files that start between "w" to "z" and with extension ".py"

find /home -name "[w-z]*.py"    

It is possible to search by date, size, combinations are many and you will find them in find help.

answered by 22.12.2015 в 00:08

Original answer from Finding all files containing a text string on Linux from rakib

Do the following:

grep -rnw '/ruta/donde/quieres/buscar'  -e "patrón"

-r or -R is recursive, -n is number of the line and -w means that it matches the word. -l (letter L) can be added to get the name of the file.

Along with this, the parameters --exclude or --include could be used for an efficient search.

Something like the following:

grep --include=\*.{c,h} -rw '/ruta/donde/quieres/buscar' -e "patrón"

This will only search among files that have the extension .c or .h. In the same way an example of the use of --exclude:

grep --exclude=*.o -rw '/ruta/donde/quieres/buscar' -e "patrón"

The above will exclude from the search the files that end with .o extension. In the same way you can exclude / include directories through the parameter --exclude-dir and --include-dir, the following shows how to integrate --exclude-dir:

grep --exclude-dir={dir1,dir2, *.dst} -rw '/ruta/donde/quieres/buscar' -e "patrón"

This worked very well for me, to get almost the same as you propose.

For more options:

man grep
answered by 21.12.2015 в 22:25

You can try to use the following:

grep    options         pattern          input_file_names 

grep    -Rl     "cadenaDeTextoABuscar"         ./


grep -Rl  "cadenaDeTextoABuscar" ./


the use of R is to indicate that the search will be recursively and l to show the name of the file containing the indicated string

You can look at the manual for other options (link in English).


answered by 21.12.2015 в 22:51

Assuming that in the relative path where we are, there is a file with the text string "six" inside it in the same directory (or in subdirectories / directories children):

find . -name "*" | xargs grep -i "seis"
answered by 30.10.2017 в 23:57

I use this sentence:

grep -rl "string" /path


  • -r (or --recursive) is used to check the subdirectories of / path, while
  • -l (or --files-with-matches) is used only to print the file names of those who meet the condition, instead of the lines where the match was made (it even improves the speed, since grep slows the reading of the file in the first match, avoiding going through the whole file).


grep -rl "hola mundo" /var/www/html/
answered by 13.12.2017 в 17:09