6

How to find the files recursively having a text pattern excluding some directories and files?

Example:- 
$ ls
bower.json      bower_components    configure       results         unit-tests

so I need to find "searchText" recursively in all directories excluding bower.json and results

codeforester
  • 34,080
  • 14
  • 96
  • 122
nagendra547
  • 4,484
  • 1
  • 26
  • 41
  • Use a method from https://stackoverflow.com/q/4210042/798223 then use the -exec flag to grep on those files. – GKFX Aug 04 '17 at 06:29
  • Could you please provide the exact command? This is not working . find -name "*.js" -not -path results – nagendra547 Aug 04 '17 at 06:50
  • 1
    `find -type f -not -path './bower.json' -not -path './results/*' -exec grep 'searchString' -- '{}' +`, but @RomanPerekhrest's answer is better. – GKFX Aug 04 '17 at 11:29
  • Also I've flagged this question, as originally I marked it as a duplicate of the wrong thing, hopefully that should get fixed soon. – GKFX Aug 04 '17 at 11:31
  • 1
    Thanks GKFX!. I even find grep solution much better and clean approach. – nagendra547 Aug 10 '17 at 15:02

1 Answers1

4

Short grep solution:

grep -r --exclude="bower.json" --exclude-dir="results" "searchText"
RomanPerekhrest
  • 75,918
  • 4
  • 51
  • 91