0

If I have two strings, for example "class" and "btn", what is the linux command that would allow me to search for these two strings in the entire directory.

To be more specific, lets say I have directory that contains few folders with bunch of .php files. My goal is to be able to search throughout those .php files so that it prints out only files that contain "class" and "btn" in one line. Hopefully this clarifies things better.

Thanks,

Juventus
  • 661
  • 1
  • 8
  • 13
  • Possible duplicate of [Grep Search all files in directory for string1 AND string2](http://stackoverflow.com/questions/4275900/grep-search-all-files-in-directory-for-string1-and-string2) – Julien Lopez Sep 07 '16 at 14:58

3 Answers3

0
grep -r /path/to/directory 'class|btn'

grep is used to search a string in a file. With the -r flag, it searches recursively all files in a directory.

redneb
  • 21,794
  • 6
  • 42
  • 54
0

I normally use the following to search for strings inside my source codes. It searches for string and shows the exact line number where that text appears. Very helpful for searching string in source code files. You can always pipes the output to another grep and filter outputs.

grep -rn "text_to_search" directory_name/

example:

$ grep -rn "angular" menuapp
$ grep -rn "angular" menuapp | grep some_other_string 

output would be:

menuapp/public/javascripts/angular.min.js:251://# sourceMappingURL=angular.min.js.map
menuapp/public/javascripts/app.js:1:var app = angular.module("menuApp", []);
Samundra
  • 1,869
  • 17
  • 28
0

Or, alternatively using the find command to "identify" the files to be searched instead of using grep in recursive mode:

find /path/to/your/directory -type f -exec grep "text_to_search" {} \+;
ewcz
  • 12,819
  • 1
  • 25
  • 47