How to Use the Find Command to Search in Windows

windows find feature

Is Windows search too slow for you? Learn how to speed up your search using the find command in the Command Prompt window.

Windows has some built-in search capabilities, but they may not be to your liking. Cortana or the standard Search box on the Taskbar and the Search box in File Explorer in Windows 10 allow you to search through file contents, but they can be slow, especially the File Explorer search.

There’s a faster way to search the contents of files on your hard drive using the command line. The find command searches for text strings in files and returns the lines of text from the files in which the text string was found.

NOTE: The find command is not suitable for large files or large numbers of files.

Today we’ll cover how to use the find command and we’ll provide some examples.

Open the Command Prompt Window with Administrative Privileges

Opening the Command Prompt window as administrator is not necessary. However, it does help you avoid annoying confirmation dialog boxes. Just be careful what commands you run as administrator on the command line. Using the find command as administrator is safe as it doesn’t change or delete any files.

Enter cmd.exe in the Search box on the Taskbar. Then, right-click on the Command Prompt item under Best match and select Run as administrator from the popup menu.

Open Command Prompt as Administrator

If the User Account Control dialog box displays, click Yes to continue.

NOTE: You may not see this dialog box, depending on your User Account Control settings. We don’t recommend disabling UAC entirely.

UAC dialog box

Switches and Parameters for the find Command

Most commands have optional switches that modify the default operation of the command. You can get help to see all the available switches for the find command by typing the following line at the prompt and pressing Enter.

find /?

Find command help

The switches can be lowercase or uppercase.

For the “string” parameter, you must surround the string with double quotes, otherwise the find command will not work and will return an error.

The [drive:][path]filename parameter can be anything from a drive letter to a single file or multiple files.

Syntax for the find Command

A command’s syntax is a specific way to organize the command and its switches and parameters. The following is the general syntax for the find command.

find [switches] "string" [pathname/s]

The switches can be in any order as long as they’re before the “string” parameter. The brackets [] indicate that the switch or parameter is optional.

Search a Single Document for a Text String

First, we’ll show you how to search one document for all occurrences of a text string. The following command searches the example1.txt file for the phrase “groovypost is the best tech site”.

find "groovypost is the best tech site" "C:\Users\Lori\Documents\FindCommandExamples\example1.txt"

NOTE: If there are spaces in any part of the path or file name, you must put quotes around the entire path, like we did in the above command. The quotes are not really needed in this case, but it doesn’t hurt to have them.

Did not find text string

Notice that the phrase was not found in the above example (nothing is listed below the path to the file), even though it is in the file. That’s because the case in “groovypost” did not match what was in the file, which is “groovyPost”. Add the “/i” (lowercase or uppercase letter “i”) switch right after the find command (before the phrase) to ignore the case when looking for the text phrase.

find /i "groovypost is the best tech site" "C:\Users\Lori\Documents\FindCommandExamples\example1.txt"

Now, the phrase was found and the entire line containing the phrase prints to the screen below the path to the file being searched.

Text string found

Search Multiple Documents for the Same Text String

Now that you can search one file for a text string, let’s search multiple files for the same text string.

You can specify multiple files to search in the find command by putting the path to each file in quotes separated by a space.

find /i "groovypost" "C:\Users\Lori\Documents\FindCommandExamples\example1.txt" "C:\Users\Lori\Documents\FindCommandExamples\example2.txt"

You could also search all text files in a directory using the wildcard character, which is an asterisk (*), as shown in the following command.

find /i "groovypost" "C:\Users\Lori\Documents\FindCommandExamples\*.txt"

The search term was found in both documents and the sentences in which they were found are listed under the full path to each file.

Search two files

Count the Number of Lines in a File

If you want to know how many lines there are in a file, you can use a combination of the type and find commands. The type command displays the contents of one or more text files.

We piped the results of the type command into the find command using the vertical bar (|). We used the “/v” switch to display all lines NOT containing the “” string, so every line with text will be counted. To display only the number of lines in the text file (not the lines themselves), we use the “/c” switch.

type C:\Users\Lori\Documents\FindCommandExamples\example1.txt | find "" /v /c

Count number of lines

Send the Output of Another Command to the Find Command

You can also search all file names in a directory for a certain string by piping the output of the dir command to the find command.

For example, we got the directory listing of the C:\Users\Lori\Documents\FindCommandExamples directory and any subdirectories in that directory (“/s” switch). We also specified to use the bare format with no heading information or summary (“/b” switch) and to display the listing in the same format as the wide list format (“/w” switch) but sorted by column (“/d” switch).

Then, we pipe (|) the output of the dir command into the find command, only adding “example” as the parameter. We didn’t add any switches to the find command. The file names to search come from the output of the dir command.

dir "C:\Users\Lori\Documents\FindCommandExamples" /s /b /d | find "example"

Redirect dir command to find command

Do you prefer using the find command or the Search box in File Explorer? How have you used the find command? Share your ideas and examples with us in the comments below.

1 Comment

1 Comment

  1. Phil O'Sophocles

    November 28, 2017 at 5:36 am

    There is also a FINDSTR command which is more powerful than FIND because it has more options and supports regular expressions.

Leave a Reply

Your email address will not be published. Required fields are marked *


To Top