Skip to content
Advertisement

How to grep recursively and ignore subdirectories of subdirectories?

I am already familiar with grep -r "searchTerm" . and I am familiar with grep -r "searchTerm" exclude={subdir1, subdir2} . However, I am looking for a way to grep recursively, through subdirectories and ignore certain subdirectories of subdirectories.

For example, if the directory structure is as follows:

/folder
   /subdir1
      /idc/file.js
      /files/file.js
      /otherStuffIdc/file.js
   /subdir2
      /idc/file2.js
      /files/file2.js
      /otherStuffIdc/file2.js
...

How would I be able to exclude idc and otherStuffIdc, since these folders are subdirectories of subdirectories of the root directory I am beginning my search in?

My example is simplified but in my real world issue, there are many, many subDir# so it is not feasible for me to just grep each individual subdirectory.

Advertisement

Answer

This will exclude the (sub)directories with names idc and otherStuffIdc.

 grep -r pattern --exclude-dir=idc,otherStuffIdc
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement