FIND Command - Everything You Need To Know (Windows, CMD, DOS)
What is the find command?
While windows has some search capabilities from the GUI, searches are usually quite slow and they can not search within the contents of any files.
Often however it might be necessary for you to locate a file based on the text within it, such as for example when you don’t remember the name or location of a specific file but remember what was written within it.
That’s where the command find comes in. You can use the find command to search for a file based on the contents inside it. Alternatively in a similar fashion you can use the find command to find where a string is located within a single or multiple files.
The find command is especially useful for programmers, writers or people that use lots of files in a frequent basis.
An alternative to the find command is findstr. The difference being that findstr has more search options and includes support for regular expressions, which makes it similar to the grep command in linux.
How to use the find command to locate files.
To use the find command to check whether a file contains a specified string simply type “find” followed by the text you want to search for within quotes and the location or name of your text file.
Find "Awesome" "Text_Document.txt"
If the string was found within your file the command line will output the name of the file and the string below it.
If the find command did not manage to find your text even tho you are sure that it is contained within your file, try using the /i parameter, which will ignore whether or not the case of the characters in the file is different from the ones in the provided string.
Find /I "Awesome" "Text_Document.txt"
Your file or string should be located at once.
But what if we want to search within multiple files at once?
To search for a string inside multiple documents either enter the names or the locations of your text documents one after the other.
Find /I "Awesome" "Text_Document.txt" "Text_Document2.txt"
Or alternatively take advantage of wildcards to search through every text document in a directory.
Find /I "Awesome" *.txt
Using wildcards you can even restrict your search to files that contain a specific word within their name.
Find /I "Awesome" *File*.txt
In this example the find command will only search through text files that have the word “file” in their name.
How to use the find command to locate strings.
So far we have been using the find command to locate files based on their contents, but what if we want to search for where a specific word or phrase is located within a single or multiple files?
To do so we would have to use the /n parameter which shows us the number of the line in which our string was found at.
Find /N /I "Awesome" *.txt
The number of the line in which the specified word or phrase was located at will immediately appear in our output.
Since both the name of the file that the string was found at as well as the location of said string is displayed with the use of the /n parameter, you can use the find command to search for both files that contain a string as well as strings within files.
Useful parameters for the find command:
- /V Which displays every other string except the specified one.
- /C which only displays the count of lines that contain the specified string.
- /N Which displays the numbers of the displayed lines.
- /I Which ignores any mismatch between the casing of the characters in the provided string and any string located within the file.
Find command examples:
Searches for the word “Awesome” within the “Document.txt” file while ignoring any casing differences.
Find /I "Awesome" "Document.txt"
Searches every text document in the current directory for the word “Awesome”.
Find "Awesome" *.txt
Searches for the word “Awesome” within every text file in the desktop directory (replace <Username> with your computers name).
Find "Awesome" "C:\Users\<Username>\Desktop\*.txt"
Displays the number of the line in which the word “Awesome” was found at within the “Document.txt” file while ignoring any casing differences.
Find /I /N "Awesome" "Document.txt"
Displays the every line that does not contain the specified string within the “Document.txt” file.
Find /V "Awesome" "Document.txt"
Displays how many times the string was found in the specified file.
Find /C "Awesome" "Document.txt"
Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies the text string to find.
Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.