Windows recursive grep command-line
Asked Answered
M

9

245

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

Mu answered 30/3, 2009 at 16:46 Comment(4)
That's tough without powershell, why can you not install?Rustie
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.Mu
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.Hind
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.Dominion
D
283

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.
Drier answered 30/3, 2009 at 16:50 Comment(4)
findstr is a great replacement for grep. I tend to use findstr /sinp (recursive, case insensitive, skip binary files, and show line numbers)Pocketful
Unfortunately, findstr has a very limited support for regex, according to the documentation and the patterns I've tried to use.Francenefrances
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.Asymptote
They didn't want to break existing pipelines that relied on find's broken behavior.Severin
S
178
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:

Severin answered 30/3, 2009 at 22:26 Comment(8)
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?Steelwork
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:.Severin
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.Steelwork
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.Severin
findstr is a program, not a shell built-in command. So this has nothing at all to do with cmd.Natator
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.Severin
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.Echeverria
2>nul to hide warnings, e.g. Cannot Open, Unicode format, Line is too longOrnithosis
O
43

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" *.*
Overprint answered 5/8, 2015 at 19:24 Comment(1)
Thanks for providing the only example that was simple and easy to use.Gibber
F
29

Recursive search for import word inside src folder:

findstr /s import .\src\*
Floc answered 10/3, 2017 at 16:42 Comment(0)
L
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 ;)

Linguini answered 13/5, 2011 at 10:17 Comment(2)
@Linguini The question says "I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfortunately".Hines
Upvoted because it is still useful to me. Also it does not require 'installation', just extract it somewhere.Zook
G
12
for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt
Gametocyte answered 14/3, 2013 at 8:52 Comment(2)
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.Fielder
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.Tannatannage
S
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

Shericesheridan answered 31/3, 2016 at 3:18 Comment(1)
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.Flong
L
6

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

Lucid answered 9/5, 2009 at 3:53 Comment(0)
I
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[...]]"

Inflationism answered 20/10, 2022 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.