I'd use find:
mygrep() (
pattern=$1; shift
find "$@" -type d -path '*/app/lib/bower' -exec sh -c '
printf >&2 "Warning: ignoring \"%s\" directory\n" "$@"
' sh {} + -prune -o -type f -exec grep -IHne "$pattern" {} +
)
(-I and -H being GNU extensions).
Use as:
mygrep pattern dir1 dir2
(making sure dir1 and dir2 don't look like find predicates).
Remove the -exec sh...+ if you don't care about those warning messages.
It's a bad idea to use grep as your alias name when you change its behaviour so dramatically.
If you need to pass options to grep, you could use the GREP_OPTIONS variable like:
GREP_OPTIONS=-i mygrep foo dir
But note that support for that option will be removed in future versions of grep, so a better option might be to give a way in mygrep to pass options to grep like with a dedicated array if your shell supports them:
mygrep() (
pattern=$1; shift
find "$@" -type d -path '*/app/lib/bower' -exec sh -c '
printf >&2 "Warning: ignoring \"%s\" directory\n" "$@"
' sh {} + -prune -o -type f -exec \
grep -IHne "${g[@]}" "$pattern" {} +
)
g=(-i --exclude-dir=.git); mygrep foo dir1 dir2
Or use -- to tell mygrep where the options stop:
mygrep() (
grep_options=()
for i do
grep_options+=("$i")
shift
[ "$i" != "--" ] || break
done
pattern=${1?need a pattern}; shift
find "$@" -type d -path '*/app/lib/bower' -exec sh -c '
printf >&2 "Warning: ignoring \"%s\" directory\n" "$@"
' sh {} + -prune -o -type f -exec \
grep -H "${grep_options[@]}" "$pattern" {} +
)
mygrep -nI --exclude-dir=.git -- pattern dir1 dir2
(and make sure you don't pass -- as an option arguments, as in if you want to exclude the -- files, use --exclude=--, not --exclude -- for instance. That also precludes usages like mygrep -e pattern1 -e pattern2 -- dir or mygrep -f patternfile -- dir).
alias exgrep='grep --exclude-dir={lodash,paho-mqtt-js,socket-io-client}', so if you use exgrep, you know there is something excluded?