I am using grep from Gnuwin32 on Windows.


grep -r INSERT *.sql


grep: *.sql: Invalid argument

Any idea why?

  • Have you updated gnuwin32? Command appears to work correctly for me. There was an older bug that had this behavior. – monkey Oct 23 '15 at 14:40

According to the Grep manual:

-r with grep is recursive directory searching, so to use it you specify the starting directory, not a file mask.


grep -r INSERT . would look at all files for INSERT, starting at the current directory (.) and recursively working its way through the sub-folders.

To specify recursive folder checking and specify a file wildcard to limit searches, you can use the --include option:

grep -r --include "*.sql" INSERT .

Similar question/info over on StackOverflow: How do I grep recursively?

  • I think I need to update my grep. I have no --include option! But this makes a lot of sense. – Mike Oct 26 '15 at 16:54

Because there is no *.sql file.

  • While that in fact is true, adding -r yields the same error. – Mike Oct 23 '15 at 15:45
  • @Mike bash expands *.sql to all files/ directories matching this pattern. Since nothing matches, you get the error. -r doesn't have anything to do with this – fedorqui Oct 23 '15 at 15:48
  • Sorry, I don't follow. There are no files with an extension of .sql in the folder where I ran the command. There are subfolders and they do in fact have files with .sql as the extension. Shouldn't the -r help then? – Mike Oct 23 '15 at 15:56
  • TECHIE007 has a nice answer to this. Completely unrelated to your question, perhaps for fun, or for comparison, I'd like just to add the non-Gnuwin32 solution that likely does the same thing: @for /f %i in ('dir /s/b *.sql') do @(type %i | find "INSERT" > nul && if errorlevel 0 echo %i). – jan.supol Oct 23 '15 at 17:09

grep is a great tool with some interesting parameters. However, as its name says (globally search a regular expression and print) it is meant for matching things. If what you want is to find files, use find.

In this case, it looks like you want to look for the text INSERT within files that are in this tree.

For this, you need to do something like:

find -name "*.sql" grep -h INSERT {} \;

find -name "*.sql" will find all these files and then grep -h will print those having the text INSERT in them.

Why wasn't your approach working?

Because by saying grep -r ... *.sql, bash tries to expand that *.sql before performing the command. But nothing matches *.sql in your directory, so it cannot work.

You can use the --include parameter with a regexp, but -in my experience- it is quite fragile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.