How to use grep to search multiple unnested directories

grepsearch

It may sound like I'm asking the same thing as this question, but I have different requirements. This is an example of my filesystem:

  • /code/
    • internal/
      • dev/
      • main/
    • public/
      • dev/
      • main/
      • release/
    • tools/

/code/internal/dev/, /code/public/dev/ and /code/tools/ contain subdirectories for multiple projects. I work almost exclusively in the dev branches of /code/internal/ and /code/public/, and often I want to search for a text string in those directories along with /code/tools/ (which has no branches). In these instances I have to do three separate commands:

$ grep -r "some string" /code/internal/dev/
$ grep -r "some string" /code/public/dev/
$ grep -r "some string" /code/tools/

I'd like to know if there's a single command to do this. If not, I would most likely need to write a simple bash script.

Best Answer

You can concatenate several paths for grep to look for:

grep -r "some string" /code/internal/dev/ /code/public/dev/ /code/tools/
Related Question