grep does not recurse

Solution 1:

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."

Solution 2:

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.

Solution 3:

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"