Skip to content

/commands/cmd/findstr-search-text

findstr: search text in files from cmd (the Windows grep)

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
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

cmd
> 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

OptionMeaning
/icase-insensitive
/nprint line numbers
/ssearch the current folder and subfolders (with a file pattern such as *.log)
/mprint only the names of files that match
/vprint lines that do not match
/xthe whole line must match
/b, /ematch at the beginning / end of the line
/c:"text"literal phrase, spaces included
/rregular expression (default for most patterns)
/lliteral, no regex
/g:fileread 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:

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".

Sources