/commands/cmd/findstr-search-text
findstr: search text in files from cmd (the Windows grep)
- tool
- Command Prompt (cmd)
- versions
- Windows 10, Windows 11, Windows Server 2016+
- os
- windows
findstr "text" file.txt prints the lines of file.txt that contain text; it is cmd's built-in equivalent of grep. Add /i to ignore case, /n for line numbers, /s to search subfolders, /m to print only file names. Watch out for one surprise: spaces separate search terms, so findstr "disk full" matches lines with disk or full. Use /c:"disk full" for a phrase.
Examples
> app.log echo error: disk full
>> app.log echo ok
>> app.log echo Error: timeout
>> app.log echo full backup done
findstr /n "error" app.log
echo --- /i
findstr /i "error" app.log
echo --- two words without /c: means OR
findstr "disk full" app.log
echo --- /c: phrase
findstr /c:"disk full" app.log
echo --- /v lines that do not match
findstr /v /i "error" app.log
echo --- regex
findstr /r /i "^error:.*t$" app.log
findstr "nomatch" app.log
echo no match: ERRORLEVEL=%ERRORLEVEL%Output
1:error: disk full --- /i error: disk full Error: timeout --- two words without /c: means OR error: disk full full backup done --- /c: phrase error: disk full --- /v lines that do not match ok full backup done --- regex Error: timeout no match: ERRORLEVEL=1
ERRORLEVEL is 0 when at least one line matched and 1 when none did, which makes findstr usable as a test: findstr /c:"ready" log.txt > nul && echo found.
Options
| Option | Meaning |
|---|---|
| /i | case-insensitive |
| /n | print line numbers |
| /s | search the current folder and subfolders (with a file pattern such as *.log) |
| /m | print only the names of files that match |
| /v | print lines that do not match |
| /x | the whole line must match |
| /b, /e | match at the beginning / end of the line |
| /c:"text" | literal phrase, spaces included |
| /r | regular expression (default for most patterns) |
| /l | literal, no regex |
| /g:file | read search terms from a file |
Regex limits
findstr's regular expressions are not POSIX or .NET: . * ^ $ [class] [^class] \< \> work, but there is no +, ?, {n}, alternation | or grouping. For anything more, use PowerShell:
Select-String -Path *.log -Pattern 'error: (\w+)' | ForEach-Object { $_.Matches[0].Groups[1].Value }Search in command output with a pipe: tasklist | findstr /i "node", ipconfig | findstr /c:"IPv4".