grep --include command doesn't work in OSX Zsh
FreeBSD/macOS grep
does support the --include
option (see man grep
; it's unfortunate that the command-line help (grep -h
) doesn't list this option), but your problem is that the option argument, *.rb
, is unquoted.
As a result, it is your shell, zsh
, that attempts to pathname-expand --include=*.rb
up front, and fails, because the current directory contains no files with names matching glob pattern *.rb
.
grep
never even gets to execute.
Since your intent is to pass *.rb
unmodified to grep
, you must quote it:
grep --include='*.rb' -rnw . -e "pattern"
If your grep
does not support --include
, and you don't want to install GNU grep
just for this, there are a number of portable ways to perform the same operation. Off the top of my head, try
find . -type f -name '*.rb' -exec grep -nw "pattern" /dev/null {} \;
The find
command traverses the directory (like grep -r
) looking for files named *.rb
(like the --include
option) and the /dev/null
is useful because grep
shows a slightly different output format when you run it on multiple files.
This is slightly inefficient because it runs a separate grep
for each file. If it's too slow, look into xargs
(or use find -exec ... {} \+
instead of ... {} \;
if your find
supports that). This is a very common task; you should easily find thousands of examples.
You might also want to consider ack
which is a popular and somewhat more user-friendly alternative. It is self-contained, so "installation" amounts to copying it to your $HOME/bin
.