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.
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.
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.
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. 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”. 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.
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. Now, the phrase was found and the entire line containing the phrase prints to the screen below the path to the file being searched.
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. You could also search all text files in a directory using the wildcard character, which is an asterisk (*), as shown in the following command. 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.
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.
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.
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. Comment Name * Email *
Δ Save my name and email and send me emails as new comments are made to this post.