How To Find Specified Strings In Files With Find Command In Windows From Command Line With Examples?
Windows operating systems provide the tool named
find to search text files for a given term or string. Linux also have a command with the same name but it is used to search files and folders names not file contents. Windows find command is very simple and easy command to work.
Help information about the find command can be printed with the
/? option like below.
Syntax of the find command provides options , string to search and file or drive or path to be searched.
FIND [options] "string" [[drive:][path]filename[ ...]]
We will simple made a search in files. We will do not provide any option. Given term will be search in given file , path or drive. In this example we will search the term
$ find "ismail" C:\users.txt
From output we can see that the term
ismail is found. The filename also given because multi file search.
Search Multiple Files
In previous example we have searched single for for given string. Find command also supports for searching multiple file with a single command. We do not specify an option. We will just use
* for file name.
* means all files those exists given path. We can also specify a path before
$ find "ismail" *
Or we will search a given paths all files.
$ find "ismail" C:\users\ismail\*
As we see find command search all files like text, binary, picture etc.
Search Files With Specific Extension
Previous example show how to search all files without filtering them according to their extension. We can give specific extension to search. We will use
* / asterisk again but we will also specify the extension of file. In this example we will search in text files where they extension is
$ find "ismail" *.txt
Print Line Numbers
While searching terms there may be more than one occurence of the given string. We may need to print the line number of the match. This can be done with
$ find /N "ismail" *.txt
As we can se from result
USERS.TXT file line number 1 have a match
While searching case sensitivity is important. The find command by default search case sensitive. We can change this behavior and search case insensitive mode. We will use
/I option for this.
$ find /I "ISMAIL" *.txt
Print Non Matching or Reverse Match
Find command have the ability to print reverse match or printing non matching lines. Using this options may create enourmous output according to the given files or files contents. We will use
/V option for this feature.
$ find /V "ismail" *.txt