TExcelTAD
Home › Windows CMD & Batch › findstr: Search Text

findstr: Search Text

findstr searches files (or piped text) for lines that match one or more strings or simple regular expressions, and prints the matching lines. It is the Command Prompt's answer to grep, and it is how batch scripts check logs, configuration files and command output.

cmd
findstr [options] "strings" [files]

All examples on this page search the same small web-server log, created at the top of each script.

Searching for a string

cmd · try it yourself
@echo off
mkdir demo & cd demo
(
  echo GET /home 200
  echo GET /cart 500
  echo POST /cart 200
  echo get /admin 403
  echo GET /home 304
) > access.log
findstr "cart" access.log
GET /cart 500
POST /cart 200

Each matching line is printed once, unchanged. By default the search is case-sensitive and the string may appear anywhere in the line, including inside words (cart would also match shopping-cart).

/i: ignore case

cmd · try it yourself
@echo off
mkdir demo & cd demo
(
  echo GET /home 200
  echo GET /cart 500
  echo POST /cart 200
  echo get /admin 403
) > access.log
echo --- case-sensitive:
findstr "get" access.log
echo --- with /i:
findstr /i "get" access.log
--- case-sensitive:
get /admin 403
--- with /i:
GET /home 200
GET /cart 500
get /admin 403

Several words vs. an exact phrase

This is the most surprising rule of findstr: spaces separate search strings. findstr "home admin" finds lines containing home or admin. To search for a phrase that contains a space, use /c:"…":

cmd · try it yourself
@echo off
mkdir demo & cd demo
(
  echo GET /home 200
  echo GET /cart 500
  echo POST /cart 200
  echo get /admin 403
  echo GET /home 304
) > access.log
echo --- "home admin" means home OR admin:
findstr "home admin" access.log
echo --- /c:"POST /cart" is one literal phrase:
findstr /c:"POST /cart" access.log
--- "home admin" means home OR admin:
GET /home 200
get /admin 403
GET /home 304
--- /c:"POST /cart" is one literal phrase:
POST /cart 200

You can give several /c: options, which again means "any of them": findstr /c:"disk full" /c:"out of memory" app.log.

Regular expressions

findstr treats search strings as regular expressions by default (/r, implied unless you use /c: or /l for literal text). Its regex dialect is small and old:

Pattern Matches
. any one character
* zero or more of the previous character or class
^ / $ start / end of the line
[abc] [a-z] one character from the class
[^0-9] one character not in the class
\< / \> start / end of a word
\. \\ a literal dot / backslash

There is no +, ?, {n}, | or ( ) grouping. For those, use PowerShell's Select-String, which supports full .NET regular expressions.

cmd · try it yourself
@echo off
mkdir demo & cd demo
(
  echo GET /home 200
  echo GET /cart 500
  echo POST /cart 200
  echo get /admin 403
  echo GET /home 304
) > access.log
echo --- lines starting with POST:
findstr "^POST" access.log
echo --- GET requests that ended in 200:
findstr /r "^GET.*200
quot; access.log echo --- G, any characters, then 4 at the end: findstr /r "^G.*4
quot; access.log
--- lines starting with POST:
POST /cart 200
--- GET requests that ended in 200:
GET /home 200
--- G, any characters, then 4 at the end:
GET /home 304

.* means "any characters", so ^G.*4$ matches lines that start with G and end with 4. Because the search is case-sensitive, the lowercase line get /admin 403 can never match ^GET or ^G; add /i to include it. Character classes such as [45]0[0-9] (status codes 400–509) work the same way on Windows:

cmd
C:\Users\ada\demo>findstr /r " [45]0[0-9]
quot; access.log GET /cart 500 get /admin 403

Inverting, numbering and other options

Option Meaning
/i ignore case
/v print lines that do not match
/n prefix each line with its line number
/c:"text" search for the literal phrase
/r / /l strings are regular expressions / literal text
/b / /e match only at the beginning / end of a line
/x the whole line must match
/m print only the names of files that contain a match
/s search in the current folder and all subfolders
/g:file read the search strings from a file, one per line
/a:attr colour for file names and line numbers

These are easiest to see with output from a real Windows PC (same access.log as above):

cmd
C:\Users\ada\demo>findstr /v "200" access.log
GET /cart 500
get /admin 403
GET /home 304

C:\Users\ada\demo>findstr /n "cart" access.log
2:GET /cart 500
3:POST /cart 200

C:\Users\ada\demo>findstr /x "GET /cart 500" access.log
GET /cart 500

C:\Users\ada\demo>findstr /e "00" access.log
GET /home 200
GET /cart 500
POST /cart 200

Searching several files

With more than one file (or a wildcard), findstr prefixes each match with the file name, and /s walks through subfolders. /m lists only the names of matching files, which is perfect for "which files mention X?":

cmd
C:\Users\ada\project>findstr /i "todo" *.txt
notes.txt:TODO: update the README
plan.txt:todo - book the meeting room

C:\Users\ada\project>findstr /s /i /m "password" *.config *.ini
app.config
deploy\settings.ini

Using the exit code

findstr sets the exit code to 0 when something matched and 1 when nothing did. Combined with >nul (hide the matching lines) and && / ||, that gives clean yes/no checks in scripts:

cmd · try it yourself
@echo off
mkdir demo & cd demo
(
  echo GET /home 200
  echo GET /cart 500
) > access.log
findstr " 500" access.log >nul && echo ALERT: server errors found
findstr " 404" access.log >nul || echo No 404s, good.
findstr /i "admin" access.log >nul
if errorlevel 1 (echo nobody visited /admin) else (echo someone visited /admin)
ALERT: server errors found
No 404s, good.
nobody visited /admin

if errorlevel 1 is true when the exit code is 1 or higher, the traditional way to test for failure (Exit Codes & errorlevel).

Searching command output

findstr reads standard input when no file is given, so it is a natural filter at the end of a pipe:

cmd · try it yourself
@echo off
mkdir demo & cd demo
for %%n in (app.js app.css readme.md logo.png main.js) do echo x> %%n
dir /b /o:n | findstr /r "\.js
quot;
app.js
main.js

The \. escapes the dot (otherwise . means "any character") and $ anchors the match at the end, so app.json would not match either. A classic interactive use on Windows:

cmd
C:\Users\ada>ipconfig /all | findstr /i "dns"
   Primary Dns Suffix  . . . . . . . :
   DNS Servers . . . . . . . . . . . : 192.168.1.1

(Sample output; yours will differ.)

Note: findstr expects plain 8-bit text. It can't search UTF-16 files (for example many logs written by Windows PowerShell 5.1), and very long lines are cut off. In those cases, use PowerShell's Select-String.

findstr vs. grep vs. Select-String

Task CMD PowerShell bash
lines containing text findstr "x" f Select-String x f grep x f
ignore case /i default -i
invert /v -NotMatch -v
line numbers /n default -n
recursive /s Get-ChildItem -Recurse | Select-String grep -r
only file names /m | Select-Object -Unique Path grep -l

Learn more: Searching Files & Text · Bash grep · PowerShell Regex

Test yourself

What does findstr "error warning" app.log find?

  1. Lines containing the phrase 'error warning'
  2. Lines containing 'error' or 'warning'
  3. Lines containing both words
  4. Nothing; it is a syntax error

Which option prints the lines that do NOT match?

  1. /n
  2. /x
  3. /v
  4. /m

What exit code does findstr return when no line matched?

  1. 0
  2. 1
  3. 2
  4. -1