find - Search for Text in Files

Maintained on

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type fruits.txt
This is a list of fruits
Apple is included
There is also a banana
There is Apple here too
C:\users\user>

Next, use the find command to extract only the lines containing “Apple”.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find "Apple" fruits.txt
---------- FRUITS.TXT
Apple is included
There is Apple here too
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかる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

ParameterDescription
/vDisplays all lines that do not contain the specified string
/cCounts the lines that contain the specified string and displays the total
/nDisplays the file’s line number before each line
/iIgnores 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)
filenameSpecifies the file to search
/?Displays help at the command prompt
チェック
The string must always be enclosed in double quotation marks (").

find command examples

Search for a specific string in a file.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find "Tokyo" cities.txt
---------- CITIES.TXT
Tokyo is the capital of Japan
C:\users\user>

With the /i option, you can search without distinguishing between uppercase and lowercase.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find /i "tokyo" cities.txt
---------- CITIES.TXT
Tokyo is the capital of Japan
I visited tokyo last year
C:\users\user>

Display line numbers

With the /n option, the line number is displayed before each line.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find /n "Error" log.txt
---------- LOG.TXT
[5]Error: File not found
[12]Warning: An Error occurred
C:\users\user>

Display lines that do not contain the specified string

With the /v option, lines that do not contain the specified string are displayed.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find /v "Success" result.txt
---------- RESULT.TXT
Error: Processing failed
Warning: A timeout occurred
C:\users\user>

Count matching lines

With the /c option, the number of lines containing the specified string is counted.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find /c "Warning" log.txt
---------- LOG.TXT: 3
C:\users\user>

Searching multiple files

You can search multiple files at once.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>find "config" file1.txt file2.txt file3.txt
---------- FILE1.TXT
config=enabled
---------- FILE2.TXT
config_version=2.0
C:\users\user>

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>for %f in (*.log) do @find /i "ERROR" "%f"

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

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir | find "txt"
2025/10/03 10:30 1,234 readme.txt
2025/10/03 11:15 5,678 notes.txt
C:\users\user>

Search the result of the tasklist command

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tasklist | find "chrome"
chrome.exe 12345 Console 1 123,456 K
C:\users\user>

Search for running services

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sc query | find "RUNNING"
STATE : 4 RUNNING
C:\users\user>

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 codeDescription
0The search string was found
1The search string was not found
2The 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.

In preparation
The linked page is currently under construction. Please wait a moment.

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.

#Command Prompt #Batch Files #Command Line #Commands