はじめに
grepはLinuxやmacOSで文字列検索を行う代表的なコマンドです。
特に複数のファイルを対象に検索できるため、ログ解析やソースコード調査など幅広い場面で活用されています。
本記事では、grepで複数ファイルを検索する基本構文から、検索対象の指定方法、ワイルドカード検索、検索結果の表示設定、条件指定、再帰検索、エラー対処までをわかりやすく解説します。
参考:GNU grep
grepで複数ファイルを検索する基本構文
ファイル作成
cat << 'EOF' > file1.txt
apple
banana
orange
EOF
ファイル作成
cat << 'EOF' > file2.txt
grape
banana
melon
EOF
ファイル作成
cat << 'EOF' > file3.txt
apple
peach
melon
EOF
実行コマンド
grep "banana" file1.txt file2.txt file3.txt
実行結果
file1.txt:banana
file2.txt:banana
実行コマンド
grep "apple" file*.txt
実行結果
file1.txt:apple
file3.txt:apple
実行コマンド
grep -n "melon" file1.txt file2.txt file3.txt
実行結果
file2.txt:3:melon
file3.txt:3:melon
仕組み
| 要素 | 内容 |
|---|---|
| grep | テキスト検索コマンド |
| "banana" | 検索する文字列 |
| file1.txt file2.txt file3.txt | 検索対象の複数ファイル |
| file*.txt | ワイルドカードで複数ファイルを指定 |
| -n | 一致した行番号を表示 |
| ファイル名:内容 | 複数ファイル検索時は一致したファイル名も表示 |
解説
grep は複数のファイルを同時に検索でき、対象ファイルをスペース区切りで指定します。
ワイルドカード (*) を使うことで、多数のファイルをまとめて検索できます。
grepで複数ファイルの検索対象を指定する方法
ファイル作成
cat << 'EOF' > sample1.txt
Linux
grep command
error log
EOF
ファイル作成
cat << 'EOF' > sample2.txt
Windows
grep multiple files
warning log
EOF
ファイル作成
cat << 'EOF' > sample3.txt
MacOS
grep test
info log
EOF
実行コマンド
grep "grep" sample1.txt sample2.txt sample3.txt
実行結果
sample1.txt:grep command
sample2.txt:grep multiple files
sample3.txt:grep test
実行コマンド
grep "log" sample*.txt
実行結果
sample1.txt:error log
sample2.txt:warning log
sample3.txt:info log
実行コマンド
grep -n "grep" sample1.txt sample2.txt sample3.txt
実行結果
sample1.txt:2:grep command
sample2.txt:2:grep multiple files
sample3.txt:2:grep test
仕組み
| 項目 | 内容 |
|---|---|
| 検索対象指定 | ファイル名をスペース区切りで複数指定する |
| ワイルドカード指定 | sample*.txt のように複数ファイルを一括指定できる |
| 出力形式 | 一致した行の先頭にファイル名が表示される |
| -n オプション | 一致した行番号も同時に表示する |
| 処理の流れ | grepが指定された各ファイルを順番に読み込み検索する |
解説
grep は複数のファイルを同時に検索でき、どのファイルで一致したかを表示します。
ファイル数が多い場合はワイルドカードを利用すると効率的に検索できます。
grepで複数ファイルをワイルドカード指定して検索する
ファイル作成
cat << 'EOF' > app.log
INFO: Application started
ERROR: Database connection failed
INFO: Retry processing
EOF
ファイル作成
cat << 'EOF' > system.log
INFO: System boot
ERROR: Disk space low
INFO: Cleanup completed
EOF
ファイル作成
cat << 'EOF' > web.log
INFO: Request received
ERROR: Internal Server Error
INFO: Response sent
EOF
実行コマンド
grep "ERROR" *.log
実行結果
app.log:ERROR: Database connection failed
system.log:ERROR: Disk space low
web.log:ERROR: Internal Server Error
実行コマンド
grep -n "INFO" *.log
実行結果
app.log:1:INFO: Application started
app.log:3:INFO: Retry processing
system.log:1:INFO: System boot
system.log:3:INFO: Cleanup completed
web.log:1:INFO: Request received
web.log:3:INFO: Response sent
実行コマンド
grep -l "ERROR" *.log
実行結果
app.log
system.log
web.log
仕組み
| 要素 | 内容 |
|---|---|
| grep | ファイル内の文字列を検索するコマンド |
| "ERROR" | 検索対象の文字列 |
| *.log | .logで終わる複数ファイルをワイルドカード指定 |
| -n | 行番号付きで表示 |
| -l | 一致したファイル名のみ表示 |
| 検索処理 | 指定した全ファイルを順番に走査して一致行を出力 |
解説
grepではワイルドカード(*.log)を使用することで、複数ファイルを一括検索できます。ログ解析や設定ファイルの調査でよく利用される便利な方法です。
grepで複数ファイルの検索結果にファイル名を表示する
ファイル作成
cat << 'EOF' > file1.txt
apple
banana
orange
EOF
ファイル作成
cat << 'EOF' > file2.txt
grape
banana
melon
EOF
ファイル作成
cat << 'EOF' > file3.txt
apple
peach
melon
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 "melon" file1.txt file2.txt file3.txt
実行結果
file2.txt:3:melon
file3.txt:3:melon
仕組み
| 項目 | 内容 |
|---|---|
| grep | 指定した文字列を検索するコマンド |
| 複数ファイル指定 | ファイル名を複数並べることでまとめて検索できる |
| ファイル名表示 | 複数ファイルを対象にすると検索結果の先頭にファイル名が表示される |
| -H | ファイル名を常に表示する |
| -n | 一致した行番号を表示する |
| 出力形式 | ファイル名:内容 または ファイル名:行番号:内容 |
解説
grepで複数ファイルを検索すると、どのファイルで一致したか分かるようにファイル名が自動的に表示されます。
単一ファイルでもファイル名を表示したい場合は -H オプションを利用します。
grepで複数ファイルの検索結果に行番号を表示する
ファイル作成
cat << 'EOF' > file1.txt
apple
banana
orange
grape
EOF
ファイル作成
cat << 'EOF' > file2.txt
melon
banana
peach
apple
EOF
ファイル作成
cat << 'EOF' > file3.txt
kiwi
lemon
banana
EOF
実行コマンド
grep -n "banana" file1.txt file2.txt file3.txt
実行結果
file1.txt:2:banana
file2.txt:2:banana
file3.txt:3:banana
実行コマンド
grep -Hn "apple" file1.txt file2.txt file3.txt
実行結果
file1.txt:1:apple
file2.txt:4:apple
仕組み
| 項目 | 説明 |
|---|---|
| grep | テキストを検索するコマンド |
| 複数ファイル指定 | file1.txt file2.txt file3.txt のように並べて指定 |
| -n | 一致した行の行番号を表示 |
| -H | ファイル名を必ず表示 |
| 出力形式 | ファイル名:行番号:一致した行 |
解説
grep -n を使用すると、複数ファイルを検索した際に一致した行の行番号を確認できます。
-H を併用すると検索対象ファイル名も明示されるため、結果の特定が容易になります。
grepで複数ファイルから大文字・小文字を区別せず検索する
ファイル作成
cat << 'EOF' > file1.txt
Linux
Ubuntu
GREP
EOF
ファイル作成
cat << 'EOF' > file2.txt
linux server
CentOS
grep command
EOF
実行コマンド
grep -i "grep" file1.txt file2.txt
実行結果
file1.txt:GREP
file2.txt:grep command
実行コマンド
grep -i "linux" file1.txt file2.txt
実行結果
file1.txt:Linux
file2.txt:linux server
実行コマンド
grep -i "grep\|linux" file1.txt file2.txt
実行結果
file1.txt:Linux
file1.txt:GREP
file2.txt:linux server
file2.txt:grep command
仕組み
| 要素 | 内容 |
|---|---|
| grep | テキストを検索するコマンド |
| -i | 大文字・小文字を区別せず検索する |
| file1.txt file2.txt | 複数ファイルを同時に検索する |
| grep\|linux | grep または linux に一致する行を検索する |
| file名:内容 | 複数ファイル検索時は一致したファイル名も表示される |
解説
grepコマンドは検索対象のファイルを複数指定でき、-i オプションを付けることで大文字・小文字を区別せず検索できます。
複数ファイルを一度に検索できるため、ログ調査や設定ファイル確認で効率的に利用できます。
grepで複数ファイルから複数キーワードを検索する
ファイル作成
cat << 'EOF' > file1.txt
grep is a text search command
You can search across multiple files
EOF
ファイル作成
cat << 'EOF' > file2.txt
grep can search multiple keywords
Extract strings from files
EOF
ファイル作成
cat << 'EOF' > file3.txt
grep is useful for log file analysis
EOF
実行コマンド
grep -E 'grep|multiple|file' file1.txt file2.txt file3.txt
実行結果
file1.txt:grep is a text search command
file1.txt:You can search across multiple files
file2.txt:grep can search multiple keywords
file2.txt:Extract strings from files
file3.txt:grep is useful for log file analysis
実行コマンド
grep -E 'grep|multiple' file*.txt
実行結果
file1.txt:grep is a text search command
file1.txt:You can search across multiple files
file2.txt:grep can search multiple keywords
file3.txt:grep is useful for log file analysis
仕組み
| 要素 | 内容 |
|---|---|
| grep | テキスト検索を行うコマンド |
| -E | 拡張正規表現を有効化 |
| grep|multiple|file | | で複数キーワードのOR検索 |
| file1.txt file2.txt file3.txt | 検索対象の複数ファイル |
| file*.txt | ワイルドカードで複数ファイルを指定 |
解説
grepの-Eオプションを利用すると、複数キーワードをOR条件でまとめて検索できます。
複数ファイルを同時に指定すると、該当した行とファイル名が表示されます。
grepで複数ファイルから正規表現を使って検索する
ファイル作成
cat << 'EOF' > file1.txt
Linux
grep
sed
EOF
ファイル作成
cat << 'EOF' > file2.txt
awk
grep
find
EOF
ファイル作成
cat << 'EOF' > file3.txt
docker
kubernetes
grep
EOF
実行コマンド
grep -E 'grep|find' file1.txt file2.txt file3.txt
実行結果
file1.txt:grep
file2.txt:grep
file2.txt:find
file3.txt:grep
実行コマンド
grep -E '^grep$' file1.txt file2.txt file3.txt
実行結果
file1.txt:grep
file2.txt:grep
file3.txt:grep
実行コマンド
grep -En 'grep|find' file1.txt file2.txt file3.txt
実行結果
file1.txt:2:grep
file2.txt:2:grep
file2.txt:3:find
file3.txt:3:grep
仕組み
| 項目 | 内容 |
|---|---|
| grep | ファイル内の文字列を検索するコマンド |
| -E | 拡張正規表現を利用する |
| -n | 一致した行番号を表示する |
| grep|find | grep または find に一致する |
| ^grep$ | 行全体が grep の場合のみ一致する |
| 複数ファイル指定 | file1.txt file2.txt file3.txt のように複数指定できる |
| ファイル名表示 | 複数ファイル検索時は一致したファイル名も表示される |
解説
grepは複数ファイルを一度に検索でき、-E を利用すると OR 条件などの正規表現を簡潔に記述できます。
ファイル名付きで結果が表示されるため、どのファイルに一致したかをすぐに確認できます。
grepで複数ファイルから完全一致する文字列を検索する
ファイル作成
cat << 'EOF' > file1.txt
apple
orange
banana
EOF
ファイル作成
cat << 'EOF' > file2.txt
grape
apple
melon
EOF
ファイル作成
cat << 'EOF' > file3.txt
pineapple
apple
Apple
EOF
実行コマンド
grep -x "apple" file1.txt file2.txt file3.txt
実行結果
file1.txt:apple
file2.txt:apple
file3.txt:apple
実行コマンド
grep -x -n "apple" file*.txt
実行結果
file1.txt:1:apple
file2.txt:2:apple
file3.txt:2:apple
仕組み
| 項目 | 内容 |
|---|---|
| grep | テキストを検索するコマンド |
| 複数ファイル指定 | file1.txt file2.txt file3.txt のように並べて指定する |
| -x | 行全体が検索文字列と完全一致した場合のみ表示する |
| -n | 一致した行番号を表示する |
| 出力形式 | ファイル名:内容 または ファイル名:行番号:内容 |
解説
grepで複数ファイルを同時に検索すると、どのファイルに一致した文字列があるかをまとめて確認できます。
-x オプションを利用すると、部分一致ではなく完全一致のみを検索できます。
grepで複数ファイルからAND条件で文字列を検索する方法
ファイル作成
cat << 'EOF' > file1.txt
server=web01
status=active
env=prod
EOF
ファイル作成
cat << 'EOF' > file2.txt
server=web02
status=inactive
env=prod
EOF
ファイル作成
cat << 'EOF' > file3.txt
server=web03
status=active
env=dev
EOF
実行コマンド
grep -l "status=active" file*.txt | xargs grep -l "env=prod"
実行結果
file1.txt
実行コマンド
grep -l "status=active" file*.txt | xargs grep "env=prod"
実行結果
file1.txt:env=prod
仕組み
| 項目 | 内容 |
|---|---|
| 1回目のgrep | 複数ファイルから status=active を含むファイル名を抽出 |
| -l オプション | 一致したファイル名のみ表示 |
| xargs | 前段のgrep結果を次のgrepの引数として渡す |
| 2回目のgrep | 絞り込まれたファイルから env=prod を検索 |
| AND条件の実現 | 1回目と2回目のgrepで段階的に絞り込む |
解説
grep 単体にはAND条件オプションがないため、grep -l と xargs を組み合わせて実現します。
複数ファイルから複数条件で対象ファイルを特定したい場合に便利です。
grepで複数ファイルからNOT条件で文字列を検索する方法
ファイル作成
cat << 'EOF' > file1.txt
apple
orange
banana
EOF
ファイル作成
cat << 'EOF' > file2.txt
grape
orange
melon
EOF
ファイル作成
cat << 'EOF' > file3.txt
apple
kiwi
peach
EOF
実行コマンド
grep -L "orange" file1.txt file2.txt file3.txt
実行結果
file3.txt
実行コマンド
grep -v "orange" file1.txt file2.txt
実行結果
file1.txt:apple
file1.txt:banana
file2.txt:grape
file2.txt:melon
実行コマンド
grep -L "apple" file*.txt
実行結果
file2.txt
仕組み
| オプション | 仕組み | 用途 |
|---|---|---|
| -L | 指定文字列を含まないファイル名のみ表示 | 複数ファイルからNOT条件でファイル検索 |
| -v | マッチした行を除外して表示 | NOT条件で行を抽出 |
| file1.txt file2.txt file3.txt | 複数ファイルを同時に検索 | grep 複数 ファイル検索 |
解説
grep では -L を使うと「文字列を含まないファイル」を簡単に特定できます。
また、-v を使うことで複数ファイルから特定文字列を除外した行のみを抽出できます。
grepで複数ファイルを再帰的に検索する
ファイル作成
mkdir -p sample/app sample/log
ファイル作成
cat << 'EOF' > sample/app/main.txt
database_host=localhost
api_endpoint=https://example.com
EOF
ファイル作成
cat << 'EOF' > sample/app/config.txt
database_user=admin
database_password=secret
EOF
ファイル作成
cat << 'EOF' > sample/log/app.log
INFO: application started
ERROR: database connection failed
EOF
実行コマンド
grep -r "database" sample
実行結果
sample/app/config.txt:database_user=admin
sample/app/config.txt:database_password=secret
sample/app/main.txt:database_host=localhost
sample/log/app.log:ERROR: database connection failed
実行コマンド
grep -rn "database" sample
実行結果
sample/app/config.txt:1:database_user=admin
sample/app/config.txt:2:database_password=secret
sample/app/main.txt:1:database_host=localhost
sample/log/app.log:2:ERROR: database connection failed
実行コマンド
grep -rl "database" sample
実行結果
sample/app/config.txt
sample/app/main.txt
sample/log/app.log
仕組み
| オプション | 説明 |
|---|---|
| grep | 文字列を検索するコマンド |
| -r | ディレクトリ配下を再帰的に検索する |
| -n | 一致した行番号を表示する |
| -l | 一致したファイル名のみ表示する |
| "database" | 検索キーワード |
| sample | 検索対象ディレクトリ |
解説
grep -r を使用すると、複数ファイルを含むディレクトリ配下を再帰的に検索できます。
-n や -l を組み合わせることで、行番号や対象ファイルのみを効率よく確認できます。
grepで複数ファイル検索時に除外ファイルを指定する
ファイル作成
cat << 'EOT' > file1.txt
error: connection failed
info: process started
EOT
ファイル作成
cat << 'EOT' > file2.txt
warning: disk usage high
error: timeout occurred
EOT
ファイル作成
cat << 'EOT' > ignore.log
error: test data
EOT
実行コマンド
grep "error" file*.txt --exclude=file2.txt
実行結果
file1.txt:error: connection failed
実行コマンド
grep -r "error" . --include="*.txt" --exclude="file2.txt"
実行結果
./file1.txt:error: connection failed
仕組み
| 項目 | 内容 |
|---|---|
| grep | 指定した文字列を検索する |
| file*.txt | 複数ファイルをまとめて検索対象にする |
| --exclude=file2.txt | 指定ファイルを検索対象から除外する |
| -r | ディレクトリ配下を再帰的に検索する |
| --include="*.txt" | 検索対象のファイル形式を限定する |
解説
grep は複数ファイル検索時に --exclude を利用することで、不要なファイルを検索対象から除外できます。
ログファイルや一時ファイルを除外したい場合によく利用されます。
grepとfindを組み合わせて複数ファイルを検索する
ファイル作成
cat << 'EOF' > file1.txt
Linux
grep command
find command
EOF
ファイル作成
cat << 'EOF' > file2.txt
grep multiple files
search target
EOF
ファイル作成
cat << 'EOF' > file3.txt
sample text
grep test
EOF
実行コマンド
find . -type f -name "*.txt" -exec grep -H "grep" {} \;
実行結果
./file2.txt:grep multiple files
./file3.txt:grep test
./file1.txt:grep command
実行コマンド
find . -type f -name "*.txt" | xargs grep -H "grep"
実行結果
./file2.txt:grep multiple files
./file3.txt:grep test
./file1.txt:grep command
仕組み
| コマンド要素 | 役割 |
|---|---|
| find . | カレントディレクトリ以下を検索 |
| -type f | ファイルのみを対象にする |
| -name "*.txt" | .txt ファイルを検索対象にする |
| -exec grep -H "grep" {} \; | 見つかった各ファイルに対して grep を実行 |
| xargs grep -H "grep" | find の結果をまとめて grep に渡す |
| grep -H | 一致した行とファイル名を表示する |
解説
find で対象ファイルを絞り込み、grep で内容を検索することで複数ファイルを効率よく検索できます。
大量のファイルを扱う場合は xargs を利用すると実行効率が向上します。
grepで複数ファイルの検索結果を別ファイルへ保存する方法
ファイル作成
cat << 'DATA' > file1.txt
error: database connection failed
info: application started
DATA
ファイル作成
cat << 'DATA' > file2.txt
warning: disk usage high
error: memory allocation failed
DATA
ファイル作成
cat << 'DATA' > file3.txt
info: backup completed
error: network timeout
DATA
実行コマンド
grep "error" file1.txt file2.txt file3.txt > result.txt
実行コマンド
cat result.txt
実行結果
file1.txt:error: database connection failed
file2.txt:error: memory allocation failed
file3.txt:error: network timeout
仕組み
| 要素 | 内容 |
|---|---|
| grep | 指定した文字列を検索する |
| "error" | 検索対象のキーワード |
| file1.txt file2.txt file3.txt | 検索対象の複数ファイル |
| > result.txt | 検索結果をファイルへ保存するリダイレクト |
| cat result.txt | 保存された内容を表示する |
解説
grepでは複数ファイルを同時に検索でき、検索結果を>で別ファイルへ保存できます。
大量のログファイルから特定文字列を抽出して管理する際によく利用されます。
grepで複数ファイル検索時によくあるエラーと対処法
ファイル作成
cat << 'EOT' > file1.txt
error: database connection failed
info: application started
EOT
ファイル作成
cat << 'EOT' > file2.txt
warning: disk usage 80%
error: timeout occurred
EOT
ファイル作成
cat << 'EOT' > file3.txt
info: backup completed
EOT
実行コマンド
grep "error" file1.txt file2.txt file3.txt
実行結果
file1.txt:error: database connection failed
file2.txt:error: timeout occurred
実行コマンド
grep "error" file1.txt file2.txt file4.txt
実行結果
file1.txt:error: database connection failed
file2.txt:error: timeout occurred
grep: file4.txt: No such file or directory
実行コマンド
grep "error" *.log
実行結果
no output
実行コマンド
grep "error" file1.txt file2.txt file4.txt 2>/dev/null
実行結果
file1.txt:error: database connection failed
file2.txt:error: timeout occurred
仕組み
| 状況 | 原因 | 対処法 |
|---|---|---|
| No such file or directory | 指定ファイルが存在しない | ファイル名を確認する |
| grep: *.log: No such file or directory | ワイルドカードに一致するファイルがない | lsで対象ファイルを確認する |
| 結果が表示されない | 検索文字列が存在しない | 検索条件を見直す |
| エラーが多く表示される | 存在しないファイルを含んでいる | 2>/dev/nullで標準エラー出力を抑制する |
解説
grepで複数ファイルを検索すると、存在しないファイルやワイルドカードの展開失敗が原因でエラーになることがあります。
ファイルの存在確認を行い、必要に応じて 2>/dev/null を利用すると効率的に検索できます。
grepで複数ファイル検索のポイントまとめ
grepで複数ファイルを検索する際は、まず基本構文を理解することが重要です。
そのうえでワイルドカードによる対象指定や、ファイル名・行番号の表示オプションを活用すると作業効率が向上し、grepをより実践的に活用できるようになります。


