クリップボードにコピーしました!
文字列のgrep

grepでディレクトリ検索を徹底解説

updated: 2026/06/22 created: 2026/06/22

はじめに

LinuxやUnix環境でファイル内の文字列を探したいときに活躍するのがgrepです。

特にディレクトリ内の複数ファイルを対象に検索できるため、ログ解析や設定ファイルの確認など幅広い場面で利用されています。

本記事では、grepを使ったディレクトリ検索の基本から実践的な活用方法まで、初学者がつまずきやすいポイントを押さえながら解説します。

参考:GNU grep

grepでディレクトリ内の特定文字列を検索する基本構文

ファイル作成

cat << 'EOF' > input.txt sample text error message success log EOF

ファイル作成

mkdir -p logs

ファイル作成

cp input.txt logs/app.log

実行コマンド

grep -r "error" logs

実行結果

logs/app.log:error message

仕組み

項目 内容
grep テキスト検索コマンド
-r ディレクトリ配下を再帰的に検索
"error" 検索対象の文字列
logs/ 検索対象ディレクトリ
検索の流れ grepがディレクトリ内のファイルを順番に読み込み、一致した行を出力

解説

grep -r を使用すると、指定したディレクトリ配下の全ファイルから特定文字列を検索できます。
一致した行は「ファイル名:内容」の形式で表示されるため、該当箇所を素早く特定できます。

grepでディレクトリ配下を再帰的に検索する方法

ファイル作成

cat << 'EOF' > input.txt sample text error message success target keyword EOF

ファイル作成

mkdir -p logs/app

ファイル作成

cp input.txt logs/app/app.log

実行コマンド

grep -r "target" logs

実行結果

logs/app/app.log:target keyword

実行コマンド

grep -rn "target" logs

実行結果

logs/app/app.log:4:target keyword

実行コマンド

grep -r --include="*.log" "target" logs

実行結果

logs/app/app.log:target keyword

仕組み

項目 内容
grep テキストを検索するコマンド
-r ディレクトリ配下を再帰的に検索する
-n 一致した行番号を表示する
--include="*.log" 指定した拡張子のファイルのみ検索する
logs 検索対象のディレクトリ
"target" 検索する文字列

解説

grep -r を使用すると、指定ディレクトリ配下のすべてのサブディレクトリ・ファイルを再帰的に検索できます。
ログ調査や設定ファイル検索で頻繁に利用される方法です。

grepで指定ディレクトリのみを検索対象にする

ファイル作成

mkdir -p logs config

ファイル作成

cat << 'EOF' > logs/app.log INFO Start application ERROR Database connection failed INFO Retry connection EOF

ファイル作成

cat << 'EOF' > config/app.conf database=mysql port=3306 EOF

実行コマンド

grep 'ERROR' logs/app.log

実行結果

ERROR Database connection failed

実行コマンド

grep -r 'ERROR' logs

実行結果

logs/app.log:ERROR Database connection failed

実行コマンド

grep --include='*.log' -r 'ERROR' logs

実行結果

logs/app.log:ERROR Database connection failed

実行コマンド

grep -r 'database' config

実行結果

config/app.conf:database=mysql

実行コマンド

grep -r 'port' config

実行結果

config/app.conf:port=3306

仕組み

項目 内容
logs/app.log ログファイルとして使用する
config/app.conf 設定ファイルとして使用する
grep 'ERROR' logs/app.log 単一ファイル内から文字列を検索する
grep -r 'ERROR' logs logs ディレクトリ配下を再帰的に検索する
--include='*.log' .log ファイルのみを検索対象に限定する
grep -r 'database' config config ディレクトリ配下の設定情報を検索する

解説

grep は検索対象としてファイルだけでなくディレクトリも指定できます。
-r を使用するとディレクトリ配下を再帰的に検索でき、--include により対象ファイルを絞り込めます。

grepでディレクトリ内のファイル名も表示して検索する

ファイル作成

cat << 'EOF' > input.txt sample text error message success EOF

ファイル作成

mkdir -p logs

ファイル作成

cat << 'EOF' > logs/app.log application start error message application end EOF

ファイル作成

cat << 'EOF' > logs/system.log system start success system end EOF

実行コマンド

grep -r "error" logs

実行結果

logs/app.log:error message

実行コマンド

grep -rn "error" logs

実行結果

logs/app.log:2:error message

実行コマンド

grep -rH "success" logs

実行結果

logs/system.log:success

仕組み

項目 内容
grep テキストを検索するコマンド
-r ディレクトリ配下を再帰的に検索する
-H ファイル名を必ず表示する
-n 一致した行番号を表示する
"error" 検索キーワード
logs 検索対象ディレクトリ

解説

grep -r を使用するとディレクトリ内のすべてのファイルを再帰的に検索できます。

検索結果には一致した内容とファイル名が表示されるため、どのファイルに対象文字列が存在するかを素早く確認できます。

grepでディレクトリ内の一致行番号を表示する

ファイル作成

cat << 'EOF' > input.txt apple banana orange apple pie grape EOF

ファイル作成

mkdir -p sample

ファイル作成

cp input.txt sample/file1.txt

ファイル作成

cat << 'EOF' > sample/file2.txt melon apple juice peach EOF

ファイル作成

cat << 'EOF' > sample/file3.txt strawberry banana split apple tart EOF

実行コマンド

grep -rn "apple" sample

実行結果

sample/file2.txt:2:apple juice
sample/file3.txt:3:apple tart
sample/file1.txt:1:apple
sample/file1.txt:4:apple pie

実行コマンド

grep -rnH "banana" sample

実行結果

sample/file3.txt:2:banana split
sample/file1.txt:2:banana

仕組み

オプション 説明
grep 文字列を検索するコマンド
-r ディレクトリ配下を再帰的に検索する
-n 一致した行の行番号を表示する
-H ファイル名を表示する
"apple" 検索対象の文字列
sample 検索対象のディレクトリ

解説

grep -rn を使用すると、ディレクトリ内の全ファイルを再帰的に検索し、一致した行の行番号を確認できます。
ログ解析や設定ファイルの調査で頻繁に利用されるオプションの組み合わせです。

grepでディレクトリ検索時に大文字・小文字を無視する

ファイル作成

cat << 'EOF' > input.txt Hello World HELLO WORLD hello world Error Message ERROR MESSAGE error message EOF

ファイル作成

mkdir -p logs

ファイル作成

cp input.txt logs/app.log

実行コマンド

grep -ri "hello" logs

実行結果

logs/app.log:Hello World
logs/app.log:HELLO WORLD
logs/app.log:hello world

実行コマンド

grep -ri "error" logs

実行結果

logs/app.log:Error Message
logs/app.log:ERROR MESSAGE
logs/app.log:error message

仕組み

項目 内容
grep テキストを検索するコマンド
-r ディレクトリ配下を再帰的に検索
-i 大文字・小文字を区別せず検索
grep -ri "hello" logs logsディレクトリ内のファイルからhelloを大小文字無視で検索
grep -ri "error" logs logsディレクトリ内のファイルからerrorを大小文字無視で検索

解説

grep -ri を使用すると、ディレクトリ配下の全ファイルを対象に再帰検索しながら大文字・小文字を無視できます。
ログ調査や設定ファイル検索でよく利用されるオプションの組み合わせです。

grepでディレクトリ内の単語を完全一致検索する

ファイル作成

cat << 'EOF' > input.txt sample text error warning error EOF

ファイル作成

mkdir -p logs

ファイル作成

cp input.txt logs/app.log

ファイル作成

printf "error success " > logs/system.log

実行コマンド

grep -Rwx "error" logs

実行結果

logs/app.log:error
logs/app.log:error
logs/system.log:error

仕組み

項目 内容
grep テキスト検索を行うコマンド
-R ディレクトリ配下を再帰的に検索
-w 単語を完全一致で検索
-x 行全体が検索語と一致する場合のみ表示
"error" 検索対象の単語
logs 検索対象ディレクトリ

解説

grep -Rwx を使用すると、指定ディレクトリ配下のファイルを再帰的に検索し、単語の完全一致のみを抽出できます。
設定ファイルやログファイルから特定のキーワードを正確に探したい場合に便利です。

grepでディレクトリ内を正規表現で検索する

ファイル作成

cat << 'EOF' > input.txt error: connection failed info: startup complete warning: disk usage 80% error: timeout occurred EOF

ファイル作成

mkdir logs

ファイル作成

cp input.txt logs/app.log

実行コマンド

grep -Er 'error|warning' logs

実行結果

logs/app.log:error: connection failed
logs/app.log:warning: disk usage 80%
logs/app.log:error: timeout occurred

仕組み

項目 内容
grep テキスト検索コマンド
-E 拡張正規表現を使用する
-r ディレクトリ配下を再帰的に検索する
'error|warning' error または warning に一致する正規表現
logs 検索対象ディレクトリ

解説

grep -Er を利用すると、ディレクトリ配下の複数ファイルを対象に正規表現検索できます。
ログ解析や設定ファイルの一括調査でよく利用される方法です。

grepでディレクトリ内の複数キーワードを検索する

ファイル作成

cat << 'EOF' > input.txt logs/app.log:INFO Application started logs/app.log:ERROR Database connection failed logs/web.log:WARN High response time detected logs/web.log:ERROR Timeout occurred config/settings.conf:database.host=localhost config/settings.conf:database.port=5432 EOF

実行コマンド

grep -E "ERROR|database" input.txt

実行結果

logs/app.log:ERROR Database connection failed
logs/web.log:ERROR Timeout occurred
config/settings.conf:database.host=localhost
config/settings.conf:database.port=5432

実行コマンド

grep -r -E "ERROR|database" .

実行結果

./input.txt:logs/app.log:ERROR Database connection failed
./input.txt:logs/web.log:ERROR Timeout occurred
./input.txt:config/settings.conf:database.host=localhost
./input.txt:config/settings.conf:database.port=5432

仕組み

項目 内容
grep テキストを検索するコマンド
-r ディレクトリ配下を再帰的に検索
-E 拡張正規表現を使用
ERROR|database OR条件で複数キーワードを指定
. カレントディレクトリを検索対象に指定

解説

grep -E を使用すると | で複数キーワードの OR 検索ができます。

-r オプションを組み合わせることで、ディレクトリ内のファイルをまとめて検索できます。

grepでディレクトリ内の特定拡張子のみ検索する

ファイル作成

cat << 'EOF' > input.txt src/main.py:print("hello") src/app.py:def main(): src/config.yaml:version: 1.0 logs/app.log:ERROR occurred README.md:sample document EOF

実行コマンド

grep '.py:' input.txt

実行結果

src/main.py:print("hello")
src/app.py:def main():

実行コマンド

grep '.py:' input.txt | grep 'main'

実行結果

src/main.py:print("hello")
src/app.py:def main():

実行コマンド

grep -r --include="*.py" "main" .

実行結果

no output

仕組み

項目 内容
grep 指定した文字列を検索するコマンド
-r ディレクトリを再帰的に検索する
--include="*.py" .py ファイルのみを検索対象にする
"main" 検索キーワード
. カレントディレクトリ配下を検索する

解説

grepでディレクトリを検索する際は -r を使用し、--include で対象拡張子を限定できます。
不要なファイルを除外できるため、検索速度向上や結果の見やすさに役立ちます。

grepでディレクトリ検索時に特定ファイルを除外する方法

ファイル作成

cat << 'EOF' > input.txt sample text error message target string EOF

ファイル作成

mkdir logs

ファイル作成

cat << 'EOF' > logs/app.log application start error message EOF

ファイル作成

cat << 'EOF' > logs/debug.log debug mode target string EOF

ファイル作成

cat << 'EOF' > logs/exclude.log error message excluded file EOF

実行コマンド

grep -rn --exclude="exclude.log" "error message" logs

実行結果

logs/app.log:2:error message

実行コマンド

grep -rn --exclude="*.log" "target string" logs

実行結果

no output

実行コマンド

grep -rn --exclude-dir=".git" --exclude="exclude.log" "error message" .

実行結果

./input.txt:2:error message
./logs/app.log:2:error message

仕組み

オプション 仕組み
-r ディレクトリを再帰的に検索する
-n 一致した行番号を表示する
--exclude="exclude.log" 指定したファイル名を検索対象から除外する
--exclude="*.log" ワイルドカードで複数ファイルを除外する
--exclude-dir=".git" 指定ディレクトリ配下を検索対象から除外する

解説

grepでディレクトリ検索を行う場合は、--excludeを利用することで不要なファイルを検索対象から外せます。
ログファイルやバックアップファイルを除外すると、検索速度向上と結果の見やすさにつながります。

grepでディレクトリ検索時に特定ディレクトリを除外する方法

ファイル作成

cat << 'EOF' > input.txt ERROR Database connection failed INFO Application started ERROR External library error print("hello") EOF

ファイル作成

mkdir -p logs vendor src

ファイル作成

sed -n '1,2p' input.txt > logs/app.log

ファイル作成

sed -n '3p' input.txt > vendor/lib.log

ファイル作成

sed -n '4p' input.txt > src/main.py

実行コマンド

grep -r --exclude-dir=vendor "ERROR" .

実行結果

./logs/app.log:ERROR Database connection failed

実行コマンド

grep -r --exclude-dir={vendor,.git} "ERROR" .

実行結果

./logs/app.log:ERROR Database connection failed

仕組み

項目 内容
grep -r 指定ディレクトリ配下を再帰的に検索する
--exclude-dir=vendor vendor ディレクトリを検索対象から除外する
--exclude-dir={vendor,.git} 複数のディレクトリを検索対象から除外する
"ERROR" 検索する文字列
. カレントディレクトリ配下を検索する

解説

grep の --exclude-dir オプションを使用すると、特定ディレクトリを除外して再帰検索できます。

ライブラリ配置先や .git などを除外することで、不要な検索結果を防げます。

grepでディレクトリ内の一致ファイル名のみ取得する

ファイル作成

cat << 'EOF' > input.txt error warning info EOF

ファイル作成

mkdir -p logs/app logs/system

ファイル作成

cat << 'EOF' > logs/app/app.log info start error database connection failed warning memory usage high EOF

ファイル作成

cat << 'EOF' > logs/system/system.log info boot completed error disk full EOF

ファイル作成

cat << 'EOF' > logs/system/debug.log debug mode enabled EOF

実行コマンド

grep -rl "error" logs

実行結果

logs/app/app.log
logs/system/system.log

実行コマンド

grep -rl "warning" logs

実行結果

logs/app/app.log

実行コマンド

grep -ril "ERROR" logs

実行結果

logs/app/app.log
logs/system/system.log

仕組み

オプション 説明
grep テキスト検索コマンド
-r ディレクトリ配下を再帰的に検索
-l 一致した行ではなくファイル名のみ表示
-i 大文字・小文字を区別しない
"error" 検索キーワード
logs 検索対象ディレクトリ

解説

grep -rl を使用すると、ディレクトリ配下を再帰的に検索し、一致した内容を含むファイル名だけを取得できます。
ログファイルの特定や設定ファイルの検索でよく利用されます。

grepでディレクトリ内の検索結果に前後の行を表示する方法

ファイル作成

cat << 'EOF' > input.txt INFO: application start INFO: loading config ERROR: database connection failed DETAIL: host=db01 DETAIL: timeout=30 INFO: retry start INFO: retry success WARN: high memory usage ERROR: disk space low DETAIL: mount=/data INFO: cleanup completed EOF

実行コマンド

grep -A 2 "ERROR" input.txt

実行結果

ERROR: database connection failed
DETAIL: host=db01
DETAIL: timeout=30
--
ERROR: disk space low
DETAIL: mount=/data
INFO: cleanup completed

実行コマンド

grep -B 2 "ERROR" input.txt

実行結果

INFO: application start
INFO: loading config
ERROR: database connection failed
--
INFO: retry success
WARN: high memory usage
ERROR: disk space low

実行コマンド

grep -C 2 "ERROR" input.txt

実行結果

INFO: application start
INFO: loading config
ERROR: database connection failed
DETAIL: host=db01
DETAIL: timeout=30
--
INFO: retry success
WARN: high memory usage
ERROR: disk space low
DETAIL: mount=/data
INFO: cleanup completed

実行コマンド

grep -r -C 1 "ERROR" .

実行結果

./input.txt-INFO: loading config
./input.txt:ERROR: database connection failed
./input.txt-DETAIL: host=db01
--
./input.txt-WARN: high memory usage
./input.txt:ERROR: disk space low
./input.txt-DETAIL: mount=/data

仕組み

オプション 説明
-A N 一致した行の後ろを N 行表示
-B N 一致した行の前を N 行表示
-C N 一致した行の前後を N 行表示
-r ディレクトリ配下を再帰的に検索
-- 複数の検索結果ブロックの区切り

解説

grepでディレクトリ内を検索する場合は -r を使用します。検索結果の前後も確認したい場合は -A、-B、-C を組み合わせることで、エラー発生時の文脈を効率よく確認できます。

grepとfindでディレクトリ内を効率的に検索する

ファイル作成

cat << 'EOF' > input.txt apple banana orange error: file not found warning: retry EOF

実行コマンド

grep "error" input.txt

実行結果

error: file not found

実行コマンド

grep -n "warning" input.txt

実行結果

5:warning: retry

実行コマンド

find . -name "input.txt"

実行結果

./input.txt

実行コマンド

find . -type f | grep "input"

実行結果

./input.txt

仕組み

コマンド 仕組み 用途
grep "error" input.txt ファイル内から指定文字列を検索 内容検索
grep -n "warning" input.txt 一致行と行番号を表示 該当箇所特定
find . -name "input.txt" ディレクトリ配下からファイル名を検索 ファイル探索
find . -type f | grep "input" find結果をgrepで絞り込み 条件付き探索

解説

grepはファイル内容を検索し、findはファイルやディレクトリ自体を検索します。
両者を組み合わせることで、ディレクトリ内の目的の情報を効率よく見つけられます。

grepでディレクトリ検索を高速化するテクニック

ファイル作成

cat << 'EOF' > input.txt ERROR: Database connection failed INFO: Application started ERROR: Invalid user input database.host=localhost database.port=5432 EOF

ファイル作成

mkdir -p project/config project/src

ファイル作成

printf "ERROR: Database connection failed INFO: Application started " > project/app.log

ファイル作成

printf "ERROR: Invalid user input " > project/error.log

ファイル作成

printf "database.host=localhost database.port=5432 " > project/config/settings.conf

ファイル作成

touch project/src/main.py project/src/test.py

実行コマンド

grep -r "ERROR" project

実行結果

project/error.log:ERROR: Invalid user input
project/app.log:ERROR: Database connection failed

実行コマンド

grep -r --include="*.log" "ERROR" project

実行結果

project/error.log:ERROR: Invalid user input
project/app.log:ERROR: Database connection failed

実行コマンド

grep -r --exclude-dir=src "database" project

実行結果

project/config/settings.conf:database.host=localhost
project/config/settings.conf:database.port=5432

仕組み

テクニック コマンド例 仕組み 効果
再帰検索 grep -r "ERROR" project ディレクトリ配下を自動で巡回して検索 手動検索を削減
対象ファイル限定 grep -r --include="*.log" "ERROR" project .log ファイルのみ検索対象にする 不要ファイルを除外して高速化
ディレクトリ除外 grep -r --exclude-dir=src "database" project 指定ディレクトリを検索対象から外す 大量ファイルの探索時間を短縮

解説

grep -r を使うとディレクトリ全体を一括検索できます。
さらに --include や --exclude-dir を組み合わせることで、不要なファイルやディレクトリを除外し、検索速度を向上できます。

grepとディレクトリ検索のポイントまとめ

grepはディレクトリ内の文字列検索を効率化できる定番コマンドです。

基本構文を理解したうえで、再帰検索や除外設定、正規表現検索などを使い分けることで作業効率が大きく向上します。

まずは基本的な検索から試し、徐々に応用機能を活用していくことがおすすめです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

©︎ 2025-2026 running terminal commands