grep command is popularly used in Linux operating systems in order to search and filter specific text. It is used in different scenarios like filtering configuration, log, data, text files, etc. Windows provides the equivalent of the Linux grep command. The findstr command can be also used to search text inside files. The
findstr command can be used similar to the grep command for the Windows operating systems. The findstr command is provided by the MS-DOS and PowerShell command-line interfaces. Alternatively, PowerShell also provides the
findstr Command Syntax
The syntax of the findstr command is like below.
findstr OPTIONS FILE/DIRECTORY
- OPTIONS are used to enable or disable different features of the findstr command.
- FILE/DIRECTORY is used to specify single file or directory to search. This is optional.
findstr Command Help
The findstr command help information can be listed with the
/? option like below. This provides findstr options and their description.
> findstr /?
findstr Command Example
In the following example, we filter the lines that contain “PM”. The dir output is filtered with the findstr command.
>dir | findstr "PM"
Filter Open Ports
The netstat is used to list IP and ports for listening and established connections. The findstr command can be used to filter open ports.
>netstat -na | findstr LISTEN
Search File Contents with findstr
The findstr can be also used to search file contents. The best practice is using the findstr with ASCII files like text, xml, json etc. In the following example, we search the file “names.txt”.
> findstr /c:"ismail" names.txt
The search is case sensitive by default where lowercase and uppercase characters are treated as different. We can make the search case-insensitive by using the
/i option for the
> findstr /i /c:"ismail" names.txt
List Only Directories
The findstr command can be also used to list only directories. The dir command is used to list files and directories by default. We can filter the list for the directories by filtering lines that contains
> dir | findstr "DIR"