Note: This is NOT a duplicate. I am dealing with a certain instance where grep's options for excluding paths/directories do not behave in an expected way.
I am trying to use grep to recursively search my root filesystem. I am trying to exclude certain directories like /proc
, /sys
, and /usr/local
. If I use --exclude-dir=${DIR_NAME}
, then it will not search in paths below ${DIR_NAME}
, but this will also mean that it will refuse to search paths below ${DIR_NAME}
if ${DIR_NAME}
is at a level below the current working directory. As an example, consider this filesystem tree:
${PWD}/
data/
settings/
sys/
users/
developer/
projects/
my_os/
build/
src/
apps/
sys/
Makefile
extras/
sys/
apps/
os/
If I want my search to exclude ${PWD}/sys
and everything under it, grep -rl --exclude-dir=sys
will exclude it, but it will also exclude ${PWD}/data/settings/sys
and ${PWD}/users/developer/projects/my_os/src/sys
, two directories I want included in my search.
I was originally hoping that --exclude-dir
/--exclude
would be allow for regular expressions and I've found this comment indicating that they do, but it seems that regular expression syntax doesn't affect the utility's behavior (e.g, "^/(dev|proc)"
). I'm fine if those two options don't use regular expressions, but I do not want the exclusion pattern to be applied to singular path components. Is there anyway to get it to work like that?