Skip to content

FINDSTR Windows Command Guide

The Windows FINDSTR command is a powerful tool used to search for specific strings within files. It allows users to perform advanced search operations with various options and parameters. FINDSTR can search for literal strings, regular expressions, and can even perform case-sensitive searches. By using FINDSTR, users can quickly locate information within files, making it a valuable tool for troubleshooting and data analysis tasks.

FINDSTR Syntax:

Terminal window
findstr [options] [string] [filename]

Options:

OptionDescription
/BMatches pattern if at the beginning of a line.
/EMatches pattern if at the end of a line.
/LUses search string literally.
/RUses search string as a regular expression.
/SSearches subdirectories.
/ICase-insensitive search.
/C:[string]Uses specified string as a literal search string.
/G:[file]Gets search strings from the specified file.
/F:[file]Reads console input and searches specified file.
/MPrints only filenames containing the search string.
/NPrints line numbers with output.
/XSearches for whole line matches.
/VPrints non-matching lines.
/PSkips files with non-printable characters.

Parameters:

ParameterDescription
stringSpecifies the string to search for.
filenameSpecifies the file(s) to search.

FINDSTR Command Samples:

Search for a Specific String in a File

Terminal window
findstr "keyword" file.txt

Searches for the specified “keyword” in the “file.txt” file and displays the matching lines.

Terminal window
findstr /i "example" file.txt

Performs a case-insensitive search for the string “example” in the “file.txt” file.

Display Line Numbers

Terminal window
findstr /n "error" file.txt

Searches for the string “error” in the “file.txt” file and displays the line numbers of the matching lines.

Search for Multiple Strings

Terminal window
findstr "string1 string2" file.txt

Searches for both “string1” and “string2” in the “file.txt” file and displays lines containing either string.

Recursive Search in Subdirectories

Terminal window
findstr /s "pattern" *.txt

Performs a recursive search for the “pattern” in all .txt files in the current directory and its subdirectories.

Search for Whole Words

Terminal window
findstr /w "word" file.txt

Searches for the whole word “word” in the “file.txt” file, ensuring only complete words are matched.

Invert the Match

Terminal window
findstr /v "exclude" file.txt

Displays lines not containing the string “exclude” in the “file.txt” file.

FINDSTR FAQ:

How do I use findstr in Windows?

To use the findstr command in Windows, execute the following command:

Terminal window
findstr /i "search term" filename.txt

What does the /i option do in findstr?

The /i option in findstr is used to make the search case-insensitive. This means that the search will ignore the case of the text while looking for the specified pattern.

Terminal window
findstr /i "search term" filename.txt

How can I search for a specific string in multiple files using findstr?

To search for a specific string in multiple files using findstr, you can use the following command:

Terminal window
findstr "search term" *.txt

What is the purpose of the /s option in findstr?

The /s option in findstr is used to search for matching files in the current directory and all subdirectories. It helps in searching for a specific string recursively through all files.

Terminal window
findstr /s "search term" *.txt

Can I display line numbers along with the search results in findstr?

Yes, you can display line numbers along with the search results in findstr by using the /n option. This option will show the line number before each line that matches the search criteria.

Terminal window
findstr /n "search term" filename.txt

How do I search for multiple strings (OR condition) using findstr?

You can search for multiple strings using an OR condition in findstr by separating the search terms with spaces inside double quotes. This will look for lines that contain either of the specified strings.

Terminal window
findstr "string1 string2" filename.txt

Applications of the FINDSTR Command

  • Searching for specific text in files
  • Filtering the output of other commands
  • Finding specific patterns in files
  • Searching for text within directories and subdirectories
  • Using regular expressions to search for complex patterns