find - Search for Text in Files
Table of Contents
Searching for specific text in files or in command output is very useful when working with the command prompt or batch files.
This article introduces how to search for text using the find command.
Basics of the find command
The find command searches for specified text in a file or in the output of a command.
It can extract and display the lines that match the search condition.
Basic usage at the command prompt
As a sample, let’s check whether fruits.txt contains the word Apple.
First, check the contents of the fruits.txt file.
Next, use the find command to extract only the lines containing “Apple”.
In this way, running the find command displays only the lines in fruits.txt that contain “Apple”.
For details on the type command used above, see the following page.
Basic usage in a batch file
@echo off
setlocal
find "Error" log.txt
endlocal
exit
When you run this batch file, the lines containing the string “Error” in the log.txt file are displayed.
find command syntax
The basic syntax of the find command is as follows.
find [/v] [/c] [/n] [/i] [/off[line]] "string" [[drive:][path]filename[...]]
Parameter description
| Parameter | Description |
|---|---|
/v | Displays all lines that do not contain the specified string |
/c | Counts the lines that contain the specified string and displays the total |
/n | Displays the file’s line number before each line |
/i | Ignores case when searching |
/off[line] | Searches files with the offline attribute set instead of skipping them |
"string" | Specifies the string to search for (must be enclosed in double quotation marks) |
filename | Specifies the file to search |
/? | Displays help at the command prompt |
"). find command examples
Basic string search
Search for a specific string in a file.
Case-insensitive search
With the /i option, you can search without distinguishing between uppercase and lowercase.
Display line numbers
With the /n option, the line number is displayed before each line.
Display lines that do not contain the specified string
With the /v option, lines that do not contain the specified string are displayed.
Count matching lines
With the /c option, the number of lines containing the specified string is counted.
Searching multiple files
You can search multiple files at once.
Searching files with wildcards
The find command accepts wildcards (* or ?) in file names. To search every .log file in the current directory, run:
However, it does not search subfolders. To include subfolders, pass the files one at a time with for /r.
Inside a batch file, write %f as %%f. For details on the for command, see the following page.
Searching standard input
You can also omit the file name and feed the file with <. In this case the file name line (---------- FRUITS.TXT) is not displayed.
Combining with pipes
The find command can search the output of other commands by using a pipe (|).
Search the result of the dir command
Search the result of the tasklist command
Search for running services
Practical examples in batch files
Extract errors from a log file
@echo off
setlocal
echo ========================================
echo Searching the log file for errors
echo ========================================
echo.
find /n "Error" application.log
if errorlevel 1 (
echo No errors were found
) else (
echo Errors were detected
)
endlocal
exit
Check a specific item in a configuration file
@echo off
setlocal
echo Checking the database connection settings...
find "database.url" config.properties > nul
if errorlevel 1 (
echo Error: Database settings not found
exit /b 1
) else (
echo Database settings found
find "database.url" config.properties
)
endlocal
exit
Check the status of a service
@echo off
setlocal
set SERVICE_NAME=Winmgmt
sc query %SERVICE_NAME% | find "RUNNING" > nul 2>&1
if errorlevel 1 (
echo Service %SERVICE_NAME% is stopped
) else (
echo Service %SERVICE_NAME% is running
)
endlocal
exit
Exit codes
The find command returns the following exit codes depending on the result.
| Exit code | Description |
|---|---|
| 0 | The search string was found |
| 1 | The search string was not found |
| 2 | The file to search was not found, or an invalid switch was used |
You can use these exit codes to branch conditionally in a batch file.
Notes
In the search string of the find command, wildcards (* or ?) and regular expressions are not interpreted; every character is matched literally.
If you want to search with regular expressions, use the findstr command.
To include double quotation marks in the search string, you must double each quotation mark.
Example: find """quoted text""" file.txt
Differences from the findstr command
The find command is suited to simple string searches, but if you need more advanced searching, we recommend the findstr command.
Main advantages of the findstr command:
- Regular expression support
- Multiple search strings can be specified
- Recursive search under a folder (
/s) is possible
When garbled text prevents searching
The find command may not be able to search Japanese text correctly in files whose character encoding, such as UTF-8, differs from the code page.
If the search results are garbled, or lines that should match are not found, try switching the code page with the chcp command.
Summary
The find command is a handy tool for searching for specific text in files or command output.
Main features:
- Simple and easy-to-use syntax
- Case-insensitive search is possible
- Flexible searching by combining with pipes
- Well suited to automation in batch files
- Conditional branching with exit codes is possible
It can be used in many situations, such as analyzing log files, checking configuration files, and filtering command output.