2

It has been some time since I don't use linux, using windows instead. In windows whenever I want to search for a string on files and files inside subfolders I do (for example for cs files)

findstr /s /i /n "Thepattern" *.cs

lately, I found out that you can use grep in MINGW64 that is installed when using Git. So I tried

grep --color -n -r "Thepattern" *.cs

But even though I put -r the search does not include the subdirectories.

What am I doing wrong with grep? and how it should be?

EDIT: Anaksunaman provided me with the correct answer:

grep --color -n -r --include=*.cs "Thepattern"

and several other options :) Thanks!

2
  • I suggest to remove *.cs.
    – Cyrus
    Commented Feb 5, 2018 at 1:58
  • but I want to search only on cs files... Commented Feb 5, 2018 at 2:02

1 Answer 1

1

I found out that you can use grep in MINGW64 that is installed when using Git.

I am assuming you are referring to Git Bash.

What am I doing wrong with grep? And how it should be?

You should try this:

 grep --color -n -r --include=*.cs "ThePattern"

--include= should come after -r. This will limit results to files that end with ".cs". You can also specify a directory if you wish e.g.:

grep --color -n -r --include=*.cs "ThePattern" ~/some/directory/'with spaces'

In this case, ~ refers to your user profile folder on Windows.

Also, assuming "ThePattern" is a string literal, you may need to include -i to make things case insensitive e.g.:

 grep --color -n -i -r --include=*.cs "ThePattern"

Otherwise, "ThePattern" is not the same as e.g. "Thepattern" and you may not get any results.

Note that you can always use grep --help to get information on additional options.

2
  • Thank you. Your answer gave the intended result. (I checked grep documentation indeed but obviously was not clear enough for me) Commented Feb 5, 2018 at 4:04
  • Glad you got things working. Documentation isn't always the clearest, no. =) Commented Feb 5, 2018 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .