235

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)?

4
  • 1
    That's tough without powershell, why can you not install? Mar 30, 2009 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, 2009 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. Mar 30, 2009 at 19:31
  • 2
    Many versions of Grep, including Gnu Grep, offer built-in recursive search (gnu.org/software/grep/manual/…), so your search could be written as grep -i 'string' -R . which, like @NathanFellman suggests, avoids the problem of overlong commands. Jun 18, 2015 at 15:23

9 Answers 9

276

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.
4
  • 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 9, 2009 at 4:21
  • 7
    Unfortunately, findstr has a very limited support for regex, according to the documentation and the patterns I've tried to use. Jun 28, 2010 at 5: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, 2012 at 17:47
  • 6
    They didn't want to break existing pipelines that relied on find's broken behavior.
    – i_am_jorf
    Aug 2, 2013 at 22:20
165
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:

8
  • 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? Dec 29, 2011 at 8: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, 2011 at 18:18
  • 5
    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. Dec 31, 2011 at 4: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, 2011 at 19:02
  • 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 2, 2013 at 22:20
39

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" *.*
1
  • Thanks for providing the only example that was simple and easy to use.
    – Nav
    Jan 28, 2022 at 12:55
24

Recursive search for import word inside src folder:

> findstr /s import .\src\*
0
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 ;)

2
  • 6
    @mPrinC The question says "I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfortunately". Oct 27, 2011 at 8:07
  • 8
    Upvoted because it is still useful to me. Also it does not require 'installation', just extract it somewhere. Jun 16, 2014 at 7:37
12
for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt
2
  • 1
    This looks similar to this answer here serverfault.com/a/506615, but I like the fact that you pipe the answer to a file. Much easier to consume. Nov 12, 2013 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. Apr 7, 2017 at 9: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

1
  • 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, 2020 at 12:35
6

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

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

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.