Find text in several directories [duplicated]

5

I have a directory with a project:

-rw-r--r-- 1 dani staff  18K Mar  4 06:54 LICENSE
-rw-r--r-- 1 dani staff 1023 Mar 29 08:06 Makefile
-rw-r--r-- 1 dani staff  129 Mar  2 15:33 README.md
-rw-r--r-- 1 dani staff  528 Mar 14 16:05 TODO
drwxr-xr-x 3 dani staff  102 Mar 29 08:09 bin
drwxr-xr-x 4 dani staff  136 Mar 25 12:19 examples
drwxr-xr-x 5 dani staff  170 Mar  2 15:35 nbproject
drwxr-xr-x 4 dani staff  136 Mar  4 06:54 src

I need to find a function iniciarEjecucion , I found information about a command that I can use.

  • grep

Then I proceeded to look for the text:

grep iniciarEjecucion *

And he throws me the following error:

grep: bin: Is a directory
grep: examples: Is a directory
grep: nbproject: Is a directory
grep: src: Is a directory

Is it necessary to enter each directory to search for a text with grep ?

That is, my question is:

How can I search for a text, in multiple directories?

I hope you can help me.

    
asked by Daniel Mejia 31.03.2017 в 17:50
source

2 answers

4

If you look at the grep documentation (in English) , you could find the following, parameters:

  

-R, -r, --recursive

     

Read all files under each directory, recursively; this is equivalent to the -d recurse option.

Translating it to Spanish:

  

-R, -r, --recursive

     

Read all the files under each directory, recursively, it is equivalent to the -d option.

What does this mean?

That if you want to search for a text, in multiple directories and the subdirectories that are in them, you just need to do this:

grep -R iniciarEjecucion *

In this case grep will find your text in * , the% * means, all the files / directories that are in the directory where you are at the moment.

Also

You can add different parameters in grep , suppose you want to know the number of the line , or, you want to ignore uppercase and lowercase , well you could do something like this:

grep -R -i -n iniciarEjecucion *

According to the documentation of grep.

To ignore capital letters:

-i, --ignore-case
    Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX .) 

Translating it to Spanish:

-i, --ignore-case
        Ignora las apariciones de mayusculas o minusculas tanto en el patron (texto que buscas) como en los archivos de entrada. (Especificado por POSIX)

To obtain the number of the line:

-n, --line-number
    Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX .) 

Translating it to Spanish:

-n, --line-number
    Agrega el prefijo a cada linea de salida con el numero donde se encuentra en el archivo buscado. (-n es especificado por POSIX .) 
    
answered by 31.03.2017 / 18:02
source
2

You can use -r to do a recursive search

grep iniciarEjecucion * -r
    
answered by 31.03.2017 в 17:57