Whenever I use grep with gnuwin32's recurse option -r and include a glob pattern for files to search (e.g. *.c), no files in the subdirectories are searched. I am using the latest grep from gnuwin32.
Specifically, I was searching for the string "iflag" in all my c source files in a directory.
grep -r iflag *.c 4 3 Answers
Grep's -r option (which is the same as the -R, --recursive, -d recurse and --directories=recurse options) takes a directory name (or pattern) as its argument. The command you are trying to execute should be interpreted as "Starting in the current working directory recurse all directories matching the pattern *.c. In each of those directories search all files for the string iflag."
I'm not sure why the recurse flag doesn't work, but here's a workaround that works for me. The -r option takes an argument: the directory to search. To search the current directory, give it the argument .. For example
grep regexp-to-find -r . --include=*.c
Edit
This is actually the expected behavior of grep, and has nothing to do with running it on Windows. The -r option takes a directory argument. Check out HairOfTheDog's answer for why.
I find the answers given so far way too complicated. Just use:
grep -r --include="*.c" searchString .(as proposed by christangrant on StackOverflow or by HairOfTheDog in the comments above.)
If you are too lazy to type that all the time, just define a function and add it to "~/.bashrc". (A normal alias is not possible since parameters are used, as explained on StackOverflow)
rgrep() { grep -r --include="$2" "$1" .
}Now you have an easy to use recursive grep. E.g., if you want to search for "string" in all text files, use:
rgrep string "*.txt" 1