Windows recursive grep command-line

303,747

Solution 1

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.

Solution 2

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:

Solution 3

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

Solution 4

Recursive search for import word inside src folder:

> findstr /s import .\src\*

Solution 5

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

Share:
303,747

Related videos on Youtube

Andy White
Author by

Andy White

Software developer from Colorado, USA.

Updated on March 11, 2020

Comments

  • Andy White
    Andy White about 4 years

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

    • Chris Ballance
      Chris Ballance about 15 years
      That's tough without powershell, why can you not install?
    • Andy White
      Andy White about 15 years
      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.
    • Nathan Fellman
      Nathan Fellman about 15 years
      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.
    • Scott Centoni
      Scott Centoni almost 9 years
      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.
  • John Kaster
    John Kaster almost 14 years
    Unfortunately, findstr has a very limited support for regex, according to the documentation and the patterns I've tried to use.
  • martin jakubik
    martin jakubik over 12 years
    @mPrinC The question says "I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfortunately".
  • Wolfpack'08
    Wolfpack'08 over 12 years
    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?
  • jamil ahmed
    jamil ahmed over 12 years
    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:.
  • Wolfpack'08
    Wolfpack'08 over 12 years
    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.
  • jamil ahmed
    jamil ahmed over 12 years
    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.
  • yoyo
    yoyo over 11 years
    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.
  • Rich
    Rich almost 11 years
    findstr is a program, not a shell built-in command. So this has nothing at all to do with cmd.
  • jamil ahmed
    jamil ahmed almost 11 years
    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.
  • jamil ahmed
    jamil ahmed almost 11 years
    They didn't want to break existing pipelines that relied on find's broken behavior.
  • jinglesthula
    jinglesthula over 10 years
    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.
  • Rosdi Kasim
    Rosdi Kasim almost 10 years
    Upvoted because it is still useful to me. Also it does not require 'installation', just extract it somewhere.
  • Zitrax
    Zitrax over 8 years
    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.
  • Martin Greenaway
    Martin Greenaway about 7 years
    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.
  • Martin819
    Martin819 almost 4 years
    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.
  • Nav
    Nav over 2 years
    Thanks for providing the only example that was simple and easy to use.