我需要在Windows中做一个递归的grep,在Unix/Linux中就像这样:

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

或者更可取的方法:

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

我只能使用cmd.exe,所以我只有Windows内置命令。不幸的是,我无法在这个服务器上安装Cygwin或任何第三方工具,如UnxUtils。我甚至不确定我能不能安装PowerShell。只使用cmd.exe内置程序(Windows 2003 Server)有什么建议吗?


findstr可以进行递归搜索(/S),并支持某些regex语法变体(/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.

findstr /spin /c:"string" [files]

各参数含义如下:

S =递归 P =跳过不可打印字符 I =不区分大小写 N =打印行号

要搜索的字符串是你在/c后面加引号的位:


如果您安装了Perl,您可以使用ack,可以在http://beyondgrep.com/上获得。


我推荐一个很棒的工具:

原生Unix utils:

http://unxutils.sourceforge.net/ http://en.wikipedia.org/wiki/UnxUtils

只需解压缩它们,并将该文件夹放入PATH环境变量中,瞧!:)

工作就像一个魅力,有很多比只有grep;)


for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt

我刚刚用下面的命令搜索了一个文本,列出了我所有包含我指定的“搜索文本”的文件名。

C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*

Select-String最适合我。这里列出的所有其他选项(比如findstr)都不能处理大文件。

这里有一个例子:

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

注意:这需要Powershell


在src文件夹内递归搜索导入单词:

> findstr /s import .\src\*

“中/旋转/ c:“字符串”[[驱动:][路径]文件名[…]]”

类似于上面第2高的答案(by i_am_jorf on Mar 30, 2009 at 22:26),它显示了以下示例:" findstr /spin /c:"string"[文件]"

但是,运行"findstr /?"显示没有定义为的选项或参数 “[文件]”。我相信他在这里暗示的是定义要搜索哪些文件的参数,“findstr /?”描述为: “[[驱动:][路径]文件名[…]]” 它后来用以下方式定义了这一点: "[drive:][path]filename" -指定要搜索的一个或多个文件。

因此,为了不使用个人简写,我提供了findstr />定义它的方式,如果搜索某些文件: “中/旋转/ c:“字符串”[[驱动:][路径]文件名[…]]”