How do I list the files in a directory and subdirectories that contain specific text on Linux?

Asked

Viewed 6,427 times

0

How to list all files in a directory and subdirectories that contain a specific text and together the corresponding line in which the text appears, under Linux?

  • 2

    Please explain better what you want to do and what you have tried to do What operating system and more details about the problem and the environment. Beyond the expected solution.

  • 1

    Try Ack: http://beyondgrep.com/

  • I changed the question to be more specific.

5 answers

5


A tool to do this is the command grep. The syntax is grep -rin "expressao_procurada"

An example of use would be looking for the definition of a function called gerar_reportario in several Python source files.

grep -rin "^def *gerar_relatorio" *.py

The -rin is to (r)be recursive, (i)ignore the case and (n) put the line number in the console output.

  • 1

    The parameter -l is also very useful, makes the grep print only the list of files that contain the searched text, without including the text line itself. It is a cleaner output, and this list in this format is more suitable to be passed as input from other programs.

3

I did so and it worked: grep -rl "algum-texto" /caminho-do-diretório.

Where:

  • the option -r serves to inform you that you want to search in all subdirectories;
  • the option -l serves to inform you that you want to display only the file names, instead of the text lines that match your search

I hope I’ve been more helpful!

2

find /diretorio -exec grep -Fi "texto_especifico" {} \;

The command find "search" recursively on "index" all files containing the "*texto_especifico*" searched by grep.

To list the files add the option -l in charge grep

find /diretorio -exec grep -Fil "texto_especifico" {} \;

1

ack  regExp  diretorio
ack  regExp

1

  • Good answer, but when using links as a response reference always try to place a part of the content of the link :)

  • OK Silvio! thanks for the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.