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.
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
@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.logGET /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
@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:"…":
@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.
@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.*200quot; access.log
echo --- G, any characters, then 4 at the end:
findstr /r "^G.*4quot; 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:
C:\Users\ada\demo>findstr /r " [45]0[0-9]quot; access.log
GET /cart 500
get /admin 403Inverting, 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):
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 200Searching 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?":
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.iniUsing 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:
@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:
@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 "\.jsquot;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:
C:\Users\ada>ipconfig /all | findstr /i "dns"
Primary Dns Suffix . . . . . . . :
DNS Servers . . . . . . . . . . . : 192.168.1.1(Sample output; yours will differ.)
Note:
findstrexpects 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'sSelect-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?
- Lines containing the phrase 'error warning'
- Lines containing 'error' or 'warning'
- Lines containing both words
- Nothing; it is a syntax error
Which option prints the lines that do NOT match?
- /n
- /x
- /v
- /m
What exit code does findstr return when no line matched?
- 0
- 1
- 2
- -1