Skip to content
Advertisement

Exclude directory when grepping with zsh globbing

I have some file structure which contains projects with build folders at various depths. I’m trying to use zsh (extended) globbing to exclude files in build folders.

I tried using the following command and many other variants:

grep "string" (^build/)#

I’m reading this as “Any folder that doesn’t match build 0 or more times.”

However I’m still getting results from folders such as:

./ProjectA/build/…/file.mi

./ProjectB/package/build/…/file2.mi

Any suggestions?

Advertisement

Answer

This should work:

grep string (^build/)#*(.)

Explanation:

  • ^build: anything not named build
  • ^build/: any directory not named build. It will not match any other file type
  • (^build/)#: any directory path consisting out of elements that are not named build. Again, this will not match a path where the last element is not a directory
  • (^build/)#*: Any path where all but the last element must not be named build. This will also list files. It also assumes that it would be ok, if the file itself were named build. If that is not the case you have to use (^build/)#^build
  • (^build/)#*(.): Same as before, but restricted to only match normal files by the glob qualifier (.)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement