How To Find Specified Strings In Files With Find Command In Windows From Command Line With Examples? – POFTUT

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

Help information about the find command can be printed with the /? option like below.

find /?
Help
Help

Syntax

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[ ...]]

Find String

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 ismail in C:\users.txt .

$ find "ismail" C:\users.txt
Find String
Find String

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" *
Search Multiple Files
Search Multiple Files

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 .txt

$ find "ismail" *.txt
Search Files With Specific Extension
Search Files With Specific Extension

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 /N option.

$ find /N "ismail" *.txt
Print Line Numbers
Print Line Numbers

As we can se from result USERS.TXT file line number 1 have a match

LEARN MORE  strlen() Function In C and C++ Tutorial with Examples

Ignore Case

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
Ignore Case
Ignore Case

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
Print Non Matching or Reverse Match
Print Non Matching or Reverse Match

1 thought on “How To Find Specified Strings In Files With Find Command In Windows From Command Line With Examples?”

Leave a Comment