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

grepでファイル名を表示する方法を徹底解説

updated: 2026/06/21 created: 2026/06/21

はじめに

grepはLinuxやUnix系環境で文字列検索を行う代表的なコマンドです。

検索結果とあわせてファイル名を表示できるため、複数ファイルの調査やログ解析で役立ちます。

本記事では、grepでファイル名を表示するための基本から実践的な活用方法までを分かりやすく解説します。

参考: GNU grep

grepでファイル名を表示する基本構文

ファイル作成

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

実行コマンド

grep -l "banana" input.txt

実行結果

input.txt

実行コマンド

grep -l "orange" input.txt

実行結果

出力無し

仕組み

項目 内容
grep ファイル内から文字列を検索するコマンド
-l 一致した行ではなくファイル名のみを表示するオプション
"banana" 検索する文字列
input.txt 検索対象のファイル
一致あり ファイル名が表示される
一致なし 何も表示されない

解説

grep -l は検索文字列が含まれるファイル名だけを表示したい場合に使用します。
複数ファイルを対象にした検索結果の確認に便利です。

grepでファイル名のみ表示する方法

ファイル作成

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

実行コマンド

grep -l "apple" input.txt

実行結果

input.txt

実行コマンド

grep -rl "apple" .

実行結果

./input.txt

仕組み

項目 内容
grep ファイル内の文字列を検索するコマンド
-l 一致した行ではなくファイル名のみ表示する
-r ディレクトリ配下を再帰的に検索する
grep -l "apple" input.txt 指定ファイル内に apple が存在する場合、ファイル名のみ表示
grep -rl "apple" . カレントディレクトリ以下から apple を検索し、該当ファイル名のみ表示

解説

grepでファイル名のみを表示するには-lオプションを使用します。
検索文字列が存在するファイルだけを一覧化したい場合に便利です。

grepでファイル名と一致した行を同時に表示する方法

ファイル作成

cat << 'EOF' > input.txt apple banana orange apple juice EOF

実行コマンド

grep -H "apple" input.txt

実行結果

input.txt:apple
input.txt:apple juice

実行コマンド

grep --with-filename "apple" input.txt

実行結果

input.txt:apple
input.txt:apple juice

仕組み

項目 内容
grep 指定した文字列を検索して一致した行を表示する
-H 一致した行の先頭にファイル名を表示する
--with-filename -H と同じくファイル名を表示する
input.txt:apple input.txt 内で apple に一致した行
複数ファイル検索 grep -H "apple" *.txt のようにするとどのファイルで一致したか判別しやすい

解説

grepでファイル名も同時に表示したい場合は -H オプションを使用します。検索結果の先頭に ファイル名: が付与されるため、一致した行がどのファイルに存在するかを簡単に確認できます。

grepで複数ファイル検索時にファイル名を表示する方法

ファイル作成

cat << 'EOF' > file1.txt apple banana orange EOF

ファイル作成

cat << 'EOF' > file2.txt grape banana melon EOF

ファイル作成

cat << 'EOF' > file3.txt peach apple kiwi EOF

実行コマンド

grep "banana" file1.txt file2.txt file3.txt

実行結果

file1.txt:banana
file2.txt:banana

実行コマンド

grep -H "apple" file1.txt file2.txt file3.txt

実行結果

file1.txt:apple
file3.txt:apple

実行コマンド

grep -nH "apple" file1.txt file2.txt file3.txt

実行結果

file1.txt:1:apple
file3.txt:2:apple

仕組み

項目 内容
grep "文字列" 複数ファイル 複数ファイルを指定すると一致した行の先頭にファイル名を表示する
-H ファイル名の表示を強制する
-n 一致した行の行番号を表示する
file1.txt:file内容 ファイル名:一致した行 の形式で出力される

解説

grepで複数ファイルを検索すると、デフォルトで一致した行の先頭にファイル名が表示されます。

-H オプションを付けると、単一ファイル検索時でもファイル名を強制表示できます。

grepでファイル名の表示を強制する方法

ファイル作成

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

実行コマンド

grep apple input.txt

実行結果

apple

実行コマンド

grep -H apple input.txt

実行結果

input.txt:apple

実行コマンド

grep apple input.txt /dev/null

実行結果

input.txt:apple

仕組み

方法 コマンド例 ファイル名表示 仕組み
通常検索 grep apple input.txt × 検索対象が1ファイルのみのためファイル名を省略
-H オプション grep -H apple input.txt ファイル数に関係なくファイル名表示を強制
/dev/null を追加 grep apple input.txt /dev/null 複数ファイル検索と認識させてファイル名を表示

解説

grep は通常、検索対象が1ファイルの場合はファイル名を表示しません。

ファイル名の表示を強制したい場合は -H オプションを使う方法が最も分かりやすく推奨されます。

grepでファイル名を表示しない方法

ファイル作成

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

実行コマンド

grep "apple" input.txt

実行結果

apple
apple pie

実行コマンド

grep "apple" *.txt

実行結果

input.txt:apple
input.txt:apple pie

実行コマンド

grep -h "apple" *.txt

実行結果

apple
apple pie

仕組み

コマンド 動作
grep "apple" input.txt ファイルを1つ指定した場合は通常ファイル名を表示しない
grep "apple" *.txt 複数ファイル検索時は一致行の先頭にファイル名を表示する
grep -h "apple" *.txt ファイル名の表示を抑制し、一致した内容のみ表示する
grep -H "apple" input.txt 1ファイルでも強制的にファイル名を表示する

解説

grep は複数ファイルを検索すると、どのファイルで一致したか分かるようにファイル名を表示します。

-h オプションを使用するとファイル名の表示を抑制し、一致した行だけを表示できます。

grepで一致しなかったファイル名を表示する方法

ファイル作成

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

実行コマンド

grep -L "grape" input.txt

実行結果

input.txt

実行コマンド

grep -L "apple" input.txt

実行結果

出力無し

仕組み

コマンド 説明 結果
grep -L "grape" input.txt 指定文字列に一致しないファイル名を表示する input.txt
grep -L "apple" input.txt 指定文字列が存在するためファイル名を表示しない 出力なし
grep -l "apple" input.txt 指定文字列に一致したファイル名を表示する input.txt

解説

grep -L は検索文字列が見つからなかったファイル名のみを表示します。

複数ファイルを対象にした場合、一致しないファイルを効率よく特定できます。

grepで再帰検索しながらファイル名を表示する方法

ファイル作成

cat << 'EOF' > input.txt grep sample text grep target text no match EOF

ファイル作成

mkdir -p src/sub log

ファイル作成

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

ファイル作成

sed -n '2p' input.txt > src/sub/test.txt

ファイル作成

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

実行コマンド

grep -rn "grep" .

実行結果

./src/main.txt:1:grep sample text
./src/sub/test.txt:1:grep target text
./input.txt:1:grep sample text
./input.txt:2:grep target text

実行コマンド

grep -rl "grep" .

実行結果

./src/main.txt
./src/sub/test.txt
./input.txt

仕組み

項目 内容
grep 指定した文字列を検索する
-r サブディレクトリを含めて再帰検索する
-n 一致した行番号を表示する
-l 一致したファイル名のみ表示する
"grep" 検索対象の文字列
. カレントディレクトリ配下を検索対象にする

解説

grep -rn を使用すると、一致したファイル名・行番号・内容をまとめて確認できます。

ファイル名だけを表示したい場合は grep -rl を使用すると効率的です。

grepで検索対象のファイル名を絞り込む方法

ファイル作成

cat << 'EOF' > input.txt apple banana orange error: file not found warning: disk full sample.txt config.txt error.log report.txt EOF

実行コマンド

grep ".txt$" input.txt

実行結果

sample.txt
config.txt
report.txt

実行コマンド

grep -H ".txt$" input.txt

実行結果

input.txt:sample.txt
input.txt:config.txt
input.txt:report.txt

実行コマンド

grep -l "error" input.txt

実行結果

input.txt

仕組み

コマンド 仕組み 用途
grep "\.txt$" input.txt .txt で終わる行を検索して表示する ファイル名の候補を抽出
grep -H "\.txt$" input.txt 検索結果の先頭に検索元ファイル名を付与する どのファイルから見つかったか確認
grep -l "error" input.txt パターンに一致した内容ではなく、対象ファイル名のみ表示する 該当ファイル名を一覧取得

解説

grep では -H を使うと検索結果と一緒にファイル名を表示できます。
また -l を使うと一致した内容ではなく、条件に合致したファイル名だけを表示できます。

grepで特定のファイル名を除外して表示する方法

ファイル作成

cat << 'EOF' > input.txt sample.log error.log access.log debug.log system.log EOF

実行コマンド

grep -v '^error.log$' input.txt

実行結果

sample.log
access.log
debug.log
system.log

実行コマンド

grep -v 'error.log' input.txt

実行結果

sample.log
access.log
debug.log
system.log

仕組み

項目 内容
grep テキストを検索して表示するコマンド
-v 条件に一致した行を除外して表示
error.log 除外したいファイル名を指定
^ 行頭を表し、完全一致検索に利用
$ 行末を表し、完全一致検索に利用
input.txt 検索対象のファイル

解説

grep -v を使用すると、指定したファイル名を除外して表示できます。

^ と $ を組み合わせることで、ファイル名の完全一致による除外が可能です。

grepで大文字・小文字を無視してファイル名を表示する方法

ファイル作成

cat << 'EOF' > input.txt Apple banana APPLE Orange apple EOF

実行コマンド

grep -i -H "apple" input.txt

実行結果

input.txt:Apple
input.txt:APPLE
input.txt:apple

仕組み

オプション 役割
grep 指定した文字列を検索する
-i 大文字・小文字を区別せず検索する
-H 一致した行の先頭にファイル名を表示する
"apple" 検索対象の文字列
input.txt 検索対象のファイル

解説

grep -i を使用すると Apple、APPLE、apple を同じ文字列として検索できます。

-H を付けることで、一致した内容とあわせてファイル名も表示できます。

grepで正規表現に一致したファイル名を表示する方法

ファイル作成

cat << 'EOF' > input.txt apple orange error: file not found banana error: permission denied grape EOF

実行コマンド

grep -l 'error:.*' input.txt

実行結果

input.txt

実行コマンド

grep -H 'error:.*' input.txt

実行結果

input.txt:error: file not found
input.txt:error: permission denied

仕組み

項目 内容
grep テキストから条件に一致する行を検索する
'error:.*' error: で始まり、その後に任意の文字列が続く正規表現
-l 一致した行ではなく、一致したファイル名のみを表示する
-H 一致した行の先頭にファイル名を付与して表示する
input.txt 検索対象ファイル

解説

grep -l を使用すると、正規表現に一致した内容が存在するファイル名だけを表示できます。
複数ファイルを対象に検索し、条件に一致したファイルを特定したい場合に便利です。

grepで複数キーワードに一致したファイル名を表示する方法

ファイル作成

cat << 'EOF' > input.txt grep 表示 ファイル名 grepでファイル名を表示する grepコマンドの表示例 ファイル名だけを出力したい grep検索の結果を表示する EOF

実行コマンド

grep -El 'grep|表示|ファイル名' input.txt

実行結果

input.txt

実行コマンド

grep -l 'grep' input.txt | xargs grep -l '表示' | xargs grep -l 'ファイル名'

実行結果

input.txt

仕組み

コマンド 説明
grep -E 拡張正規表現を使用する
grep|表示|ファイル名 いずれかのキーワードに一致(OR検索)
-l 一致した内容ではなくファイル名のみ表示
grep -l 'grep' grep を含むファイルを抽出
xargs grep -l '表示' 抽出したファイルから 表示 を含むものを絞り込む
xargs grep -l 'ファイル名' 最後に ファイル名 を含むものを抽出する

解説

grep -El を使うと複数キーワードのいずれかに一致したファイル名を簡単に表示できます。

すべてのキーワードに一致するファイルを探す場合は、grep -l をパイプで連結して絞り込みます。

grepでfindコマンドと組み合わせてファイル名を表示する方法

ファイル作成

cat << 'EOF' > input.txt error: database connection failed info: application started error: timeout occurred info: request completed EOF

実行コマンド

find . -type f -name "*.txt" -exec grep -H "error" {} \;

実行結果

./input.txt:error: database connection failed
./input.txt:error: timeout occurred

実行コマンド

find . -type f -name "*.txt" | xargs grep -H "error"

実行結果

./input.txt:error: database connection failed
./input.txt:error: timeout occurred

仕組み

要素 内容
find 指定した条件に一致するファイルを検索する
-type f 通常ファイルのみを対象にする
-name "*.txt" .txt ファイルを検索する
grep 指定した文字列を検索する
-H 検索結果の先頭にファイル名を表示する
-exec grep -H "error" {} \; findで見つかったファイルごとにgrepを実行する
xargs grep -H "error" findの結果をgrepへ渡して検索する

解説

grep -H を使用すると、検索結果の先頭にファイル名を表示できます。

find と組み合わせることで、複数ファイルから一致した文字列とファイル名を効率よく確認できます。

grepで表示したファイル名だけを抽出する方法

ファイル作成

cat << 'EOF' > input.txt apple orange banana apple juice EOF

ファイル作成

cp input.txt input2.txt

ファイル作成

echo "grape" > input3.txt

実行コマンド

grep "apple" input.txt

実行結果

apple
apple juice

実行コマンド

grep -l "apple" input.txt

実行結果

input.txt

実行コマンド

grep -l "apple" input*.txt

実行結果

input.txt
input2.txt

仕組み

オプション 説明 出力内容
grep "apple" input.txt パターンに一致する行を表示 該当行
grep -l "apple" input.txt 一致したファイル名のみ表示 ファイル名
grep -l "apple" input*.txt 複数ファイルを対象に検索 一致したファイル名一覧

解説

grep -l の -l は「files with matches」を意味し、検索結果の行ではなく一致したファイル名だけを出力します。

複数ファイルを検索する際に、条件に一致するファイルを特定したい場合に便利です。

grepでファイル名表示がされない原因と対処法

ファイル作成

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

実行コマンド

grep apple input.txt

実行結果

apple

実行コマンド

grep apple *.txt

実行結果

input.txt:apple

実行コマンド

grep -H apple input.txt

実行結果

input.txt:apple

実行コマンド

grep -h apple *.txt

実行結果

apple

仕組み

コマンド ファイル名表示 説明
grep apple input.txt × 検索対象が1ファイルのみのためファイル名を省略
grep apple *.txt 複数ファイル検索として扱われるためファイル名を表示
grep -H apple input.txt -H オプションで常にファイル名を表示
grep -h apple *.txt × -h オプションでファイル名表示を抑制

解説

grep は検索対象が1ファイルだけの場合、通常はファイル名を表示しません。

ファイル名を必ず表示したい場合は -H オプションを付与して実行します。

grepでファイル名を表示する方法のまとめ

grepはオプションを組み合わせることで、ファイル名の表示方法を柔軟に制御できます。

基本構文を理解したうえで、再帰検索や正規表現、findとの連携を活用すると検索効率が大きく向上します。

用途に応じて適切なオプションを選択し、grepをより実践的に活用していきましょう。

コメントを残す

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

©︎ 2025-2026 running terminal commands