Find all files containing specific text or string on Linux

To find all files containing a specific text or string on Linux, you can use the grep command. Here’s the basic syntax:

bash
grep -r "search_text" /path/to/search

In this command:

  • -r tells grep to search recursively through all subdirectories of the specified path
  • "search_text" is the text or string you want to search for
  • /path/to/search is the path to the directory you want to search in. You can specify a relative or absolute path.

For example, if you want to search for the string “example” in all files within the current directory and its subdirectories, you would use the following command:

perl
grep -r "example" .

If you want to limit the search to files with a certain extension, you can use the --include option. For example, to search for the string “example” only in .txt files, you would use:

perl
grep -r --include="*.txt" "example" .

This will search for “example” only in files with the .txt extension in the current directory and its subdirectories.