CMD Master
Back to Blog
UpdatedArnošt Havelka

How to Search Text in Files with findstr

findstr is the Command Prompt way to search file contents. Recurse with /s, ignore case with /i, and compare findstr vs find vs grep.

Start Interactive Lesson
How to Search Text in Files with findstr

If you want search text in files from Command Prompt, findstr is the tool. It looks inside files (not just filenames), can walk subfolders with /s, and supports a limited regular-expression syntax. Microsoft documents it as findstr. Practice in the findstr lesson.

Try the command

Search the seeded files and compare literal and regex matches

Build the command
findstr
Terminal
C:\Users\Student>findstr /n "TODO" main.js
1:// TODO: refactor this function

What findstr searches

findstr "error" app.log prints every line in app.log that contains error. Quote the pattern when it has spaces. Several words in one quoted string are treated as separate search strings (OR): findstr "Error Warning" app.log matches lines with Error or Warning.

/n prefixes line numbers. /i ignores case. /s walks the current folder and subfolders.

Terminal
C:\Users\Student>findstr /s /n "TODO" *.js

Regular-expression mode is the default. /r makes that intent explicit, while /l treats the search strings literally. ^ is start of line and $ is end of line. This is not full Perl/JavaScript regex, so a pattern that works in grep can fail here.

Terminal
C:\Users\Student>findstr /r "^[0-9]" data.txt

findstr vs find vs grep

ToolBest for
findOne literal string, simple /i /c /n
findstrSeveral files, /s recursion, light regex
grep (Linux / Git Bash)Full regex and Unix pipelines

People coming from Linux often type grep -r. In cmd, the closest everyday command is findstr /s /i "text" *.log.

Common mistake

Pasting a full grep regex into findstr /r and assuming every construct works. Start with a literal string. Add /r only for ^, $, and simple classes.

FAQ

How do I search file contents in CMD? findstr "text" file.txt

How do I search a folder and subfolders? findstr /s "text" *.*

What is the difference between find and findstr? find is a single literal string. findstr adds /s, several strings, and limited regex.

Is findstr the Windows grep? Close enough for line search. It is not GNU grep.

How do I search an exact phrase? Quote it: findstr /c:"exact phrase" file.txt when you need a literal phrase as one string.

Knowledge Check

1 / 3

Which flag searches subdirectories too?

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

Practical: Directory Ops

Real-world scenario: Setting up a project structure.