Do a man grep
on your system, and see what version you have. Your version of grep may not be able to use --exclude-dirs
.
You're really better off using find
to find the files you want, then use grep
to parse them:
$ find . -name '.git' -type d -prune \
-o -name "*.min.*" -prune \
-o -type f -exec grep --color -n -H {} "$pattern" \;
I'm not a fan of the recursive grep
. Its syntax has become bloated, and it's really unnecessary. We have a perfectly good tool for finding files that match a particular criteria, thank you.
In the find
program, the -o
separate out the various clauses. If a file has not been filtered out by a previous -prune
clause, it is passed to the next one. Once you've pruned out all of the .git
directories and all of the *.min.*
files, you pass the results to the -exec
clause that executes your grep command on that one file.
Some people prefer it this way:
$ find . -name '.git' -type d -prune \
-o -name "*.min.*" -prune \
-o -type f -print0 | xargs -0 grep --color -n -H "$pattern"
The -print0
prints out all of the found files separated by the NULL character. The xargs -0
will read in that list of files and pass them to the grep
command. The -0
tells xargs
that the file names are NULL separated and not whitespace separated. Some xargs
will take --null
instead of the -0
parameter.