241

I need to do a recursive grep in Windows, something like this in Unix/Linux:

grep -i 'string' `find . -print`

or the more-preferred method:

find . -print | xargs grep -i 'string'

I'm stuck with just cmd.exe, so I only have Windows built-in commands. I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfortunately. I'm not even sure I can install PowerShell. Any suggestions using only cmd.exe built-ins (Windows 2003 Server)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy White
  • 86,444
  • 48
  • 176
  • 211
  • 1
    That's tough without powershell, why can you not install? – Chris Ballance Mar 30 '09 at 16:49
  • 1
    The sys admins are locking down permissions on our servers. If anyone has any powershell suggestions, throw them out, and I'll see if we can install PowerShell. – Andy White Mar 30 '09 at 16:50
  • 4
    btw, I've found that in linux it's better to write: "find . |xargs grep -i string". The difference is that if find returns a very long list, you might exceed the maximum command length (it's happened to me), and you won't be able to grep at all. with xargs grep is called once per found file. – Nathan Fellman Mar 30 '09 at 19:31
  • 2
    Many versions of Grep, including Gnu Grep, offer built-in recursive search (http://www.gnu.org/software/grep/manual/grep.html#File-and-Directory-Selection), so your search could be written as `grep -i 'string' -R .` which, like @NathanFellman suggests, avoids the problem of overlong commands. – Scott Centoni Jun 18 '15 at 15:23

9 Answers9

279

findstr can do recursive searches (/S) and supports some variant of regex syntax (/R).

C:\>findstr /?
Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
             subdirectories.
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See "color /?"
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename
             Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed
with /C.  For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y.  'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurrences of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz    Word position: beginning of word
  xyz\>    Word position: end of word

For full information on FINDSTR regular expressions refer to the online Command
Reference.
Zitrax
  • 19,036
  • 20
  • 88
  • 110
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 52
    findstr is a great replacement for grep. I tend to use findstr /sinp (recursive, case insensitive, skip binary files, and show line numbers) – Steve Rowe May 09 '09 at 04:21
  • 7
    Unfortunately, findstr has a very limited support for regex, according to the documentation and the patterns I've tried to use. – John Kaster Jun 28 '10 at 05:03
  • 4
    Sigh, trust Microsoft to add a new utility (findstr) instead of fixing an existing one (find). If you wish findstr could count lines, then use this -- findstr [options] | find /c /v "" -- use findstr to match lines and find to count them. Yes, find considers that no line matches an empty string, so with /v every line will match. – yoyo Nov 19 '12 at 17:47
  • 6
    They didn't want to break existing pipelines that relied on find's broken behavior. – i_am_jorf Aug 02 '13 at 22:20
173
findstr /spin /c:"string" [files]

The parameters have the following meanings:

  • s = recursive
  • p = skip non-printable characters
  • i = case insensitive
  • n = print line numbers

And the string to search for is the bit you put in quotes after /c:

assylias
  • 321,522
  • 82
  • 660
  • 783
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • 3
    Sorry. Could you add an example? What's `spin`? Is it the line of text to find? And isn't /g or /f used to specify files? So what's with the square braces? – Wolfpack'08 Dec 29 '11 at 08:00
  • 7
    `findstr /?` explains each parameter. s = recursive, p = skip non-printable characters, i = case insensitive, n = print line numbers. You don't necessarily need all of those, but I like them, and `spin` is easy to remember. The string to search for is the bit you put in quotes after `/c:`. – i_am_jorf Dec 30 '11 at 18:18
  • 6
    Oh haha. I did a `/?`, but I actually didn't know that modifiers were used like `/spin`. I thought they were used like `/s/p/i/n`. – Wolfpack'08 Dec 31 '11 at 04:25
  • 5
    Yeah, in general. Some cmd programs let you slack off wrt the `/`s. This is one. Not all of them let you do that. You know, cmd is very special. – i_am_jorf Dec 31 '11 at 19:02
  • `findstr` is a program, not a shell built-in command. So this has nothing at all to do with `cmd`. – Joey Aug 02 '13 at 05:24
  • 3
    It is however distributed as part of Windows and, thus, meets the original posters requirements of something that can be done with cmd.exe that doesn't require any additional software to be installed. – i_am_jorf Aug 02 '13 at 22:20
  • 2
    Coming from unix I at first thought it was enough to specify . or a named directory, but apparently you need to include a * to get it to recursively search through all files. – Zitrax Nov 18 '15 at 09:58
  • `2>nul` to hide warnings, e.g. Cannot Open, Unicode format, Line is too long – Bob Stein Mar 02 '23 at 23:22
40

I just searched a text with following command which listed me all the file names containing my specified 'search text'.

C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*
khichar.anil
  • 4,585
  • 1
  • 23
  • 21
28

Recursive search for import word inside src folder:

findstr /s import .\src\*
Tonatio
  • 4,026
  • 35
  • 24
15

I recommend a really great tool:

native unix utils:

Just unpack them and put that folder into your PATH environment variable and voila! :)

Works like a charm, and there are much more then just grep ;)

mPrinC
  • 9,147
  • 2
  • 32
  • 31
12
for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt
MPelletier
  • 16,256
  • 15
  • 86
  • 137
goldenBoy
  • 121
  • 1
  • 2
  • 1
    This looks similar to this answer here http://serverfault.com/a/506615, but I like the fact that you pipe the answer to a file. Much easier to consume. – jinglesthula Nov 12 '13 at 19:56
  • 1
    Just for reference, with findstr, you get highlighted filenames on the command line, and you don't get this in a text file (obviously). So it's not a given that the text file is a more useful format. – Martin Greenaway Apr 07 '17 at 09:00
10

Select-String worked best for me. All the other options listed here, such as findstr, didn't work with large files.

Here's an example:

select-string -pattern "<pattern>" -path "<path>"

note: This requires Powershell

blizz
  • 4,102
  • 6
  • 36
  • 60
  • 2
    For me it was necessary to add `"*.*"` to the end of the path. Example: `"C:\path\to\folder\*.*"` Otherwise I was getting "Access denied" errors. – Martin819 Jul 15 '20 at 12:35
6

If you have Perl installed, you could use ack, available at http://beyondgrep.com/.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
0

"findstr /spin /c:"string" [[drive:][path]filename[...]]"

Similar to the 2nd highest answer above (by i_am_jorf on Mar 30, 2009 at 22:26) which shows the following example: "findstr /spin /c:"string" [files]"

However, running "findstr /?" shows there is no option or parameter defined as "[files]". I believe what he is implying here is the parameter that defines which files to search for which "findstr /?" describes as: "[[drive:][path]filename[ ...]]" It later defines this with the following: "[drive:][path]filename" - Specifies a file or files to search.

So, to not use personal short-hand I am providing it the way that findstr /> defines it if searching for certain files: "findstr /spin /c:"string" [[drive:][path]filename[...]]"