2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

findとgrepを組み合わせたファイル検索に便利なコマンド集

2
Last updated at Posted at 2025-06-03

これは?

ログファイルの検索などでfindとgrepを使うことがありますが、いつも使い方を忘れるので、自分用のメモを兼ねて便利なコマンドをまとめました。

特定ディレクトリ以下の全ファイルから文字列検索

find /var/log -type f -exec grep "error" {} \;

/var/log 以下の全てのファイルから error を検索します

ファイル名の条件付きで文字列を検索

find ./logs -name "*.log" -type f -exec grep "fatal" {} \;

カレントディレクトリ下の .log 拡張子のファイル内で fatal を検索します

該当箇所のファイル名を表示する(grep -l使用)

find /var/www -type f -exec grep -l "Timeout" {} \;

マッチしたファイル名のみ表示しますします

大文字小文字を無視して検索

find ./app -type f -exec grep -i "Failed" {} \;

大文字小文字を区別しないで検索します

検索した結果の前後の行も表示

find ./logs -type f -exec grep -A3 -B3 "Exception" {} \;

該当行の前後3行ずつ表示します

複数キーワードを同時に検索(正規表現)

find . -type f -exec grep -E "error|fatal|critical" {} \;

複数のキーワードをOR検索します

巨大なログを扱う場合

find ./logs -type f -name "*.log" -exec grep -H -e "timeout" -- {} +

findで見つかったファイルをまとめてgrepn渡すことで、高速かつ効率的にログ検索します

直近24時間に変更されたファイルから検索

find /var/log -type f -mtime -1 -exec grep "error" {} \;

最近のログを優先して検索します

テキストファイルのみを対象に検索

find . -type f -exec grep -I "error" {} \;

バイナリファイルを自動的に除外します

最後に

他にもあれば追記していく予定です。

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?