findstr tool to search file contents. These file contents expected to be text and string but binary binary files are accepted too. But searching binary files and content will not give good results. In this tutorial we will look different usage types of the findstr command. Keep in mind that this is different than Windows find command which can be found following tutorial.
Help about findstr command can be get with
$ findstr /?
Syntax of the findstr command is like below.
findstr [OPTIONS] [TERM] [FILENAME]
Search String In A File
The most basic usage of findstr searching a term in a file. This search will use default options where we will look them next steps. In this example we will only provide the string and file name. In this example we will search
ismail in file
$ findstr "ismail" users.txt
As we can see we provided the search term or string in double quotes to prevents errors and misuses.
While searching terms the default behaviour is case sensitive. Case sensitive means all provided string upper and lower cases are searched according to their cases and no case change will be made. We can change this behaviour according to our needs and search terms in case sensitive so given search term will match all cases accordingly.
In this example we will search for
ISMAIL in a in case sensitive option.
$ findstr /I "ISMAIL" users.txt
Search Multiple Strings
In previous example we have provided single term to search. In some situations we may need to search multiple terms. We can provide multiple strings to findstr command to search. All given strings will be like OR. In this example we will search terms
$ findstr "ismail john" users.txt
Use Regular Expression
Regular expressions are used to specify the structure of the string not the whole characters of the string. We can express a string start and end characters. These regex expressions can be used with findstr command. In this example we will search a string which starts with
j and ends with
n . We will enable regular expression search
$ findstr /R "j.*n" users.txt
Search In Multiple Files
Up to now we have searched terms in a single files. Searching single file is not efficient in most situations. Findstr provides the feature to search multiple files. In order to search multiple files we can use
* . If we want to search only text files we can also provide extension like
*.txt like below.
$ findstr "a" *.txt
Search In Multiple Files Recursively
In previous example we have searched all files in current working directory. But there is an options where we will search in all current working directories and sub directories too. We can this as recursive search. We will search for
a recursively in this example.
$ findstr /s "a" *.txt
Search At The Beginning Of Line
We can search a term or string at the end of the lines. We will use
/B options to search begging of the line. In this example we will search
is at the beginning of the line.
$ findstr /B "is" *.txt
Search At The End Of Line
Another way to search file is searching at the end of the file. We will use
/E option to search at the end of the file.
$ findstr /E "is" *.txt
Print Line Numbers
Another useful feature of the findstr is printing line numbers with the results. This will give hint about the matched string line number. We can print line number of the matched string with
/N option like below.
$ findstr /N "is" *.txt