Issue
I am following the best answer on How do I find all files containing specific text on Linux? to search string in my project.
This is my command grep --include=*.rb -rnw . -e "pattern"
Zsh tells me that zsh: no matches found: --include=*.rb
It seems that grep doesn't support --include
option.
When I type grep --help
, it returns
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
no --include
here.
Is my grep version too old? Or is there something wrong with my command?
Solution
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"
To include multiple globs:
Pass an
--include
option for each; e.g.:grep --include='*.rb' --include=='*.h*' -rnw . -e "pattern"
Alternatively, in shells that support brace expansion - notably
bash
,ksh
, andzsh
- you can let your shell create these multiple options for you, as follows - note the selective quoting (see this answer for a detailed explanation):grep '--include=*.'{rb,'h*'} -rnw . -e "pattern"
Answered By - mklement0 Answer Checked By - Marilyn (WPSolving Volunteer)