find - Search for Text in Files
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”.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
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 does not accept wildcards (* or ?) in file names. If you run something like find "ERROR" *.log, you get a “File not found” error.
To search multiple files at once, pass the files one at a time with the for command.
Inside a batch file, write %f as %%f. For details on the for command, see the following page.
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
Search multiple log files for entries on a specific date
@echo off
setlocal
set SEARCH_DATE=2025-10-03
echo Searching for log entries of %SEARCH_DATE%...
echo.
for %%f in (*.log) do (
echo [Searching %%f]
find "%SEARCH_DATE%" "%%f"
echo.
)
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
The find command does not allow wildcards (* or ?) in either the search string or the file name.
If you want to search with regular expressions or wildcards, 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
- Wildcards can be used in file names
- 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.