6
5

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 5 years have passed since last update.

grep等の探索系コマンドメモ

Posted at

grep等の探索系コマンドメモ

ログ探索等でよく使うのに忘れがちな奴のメモ

現在のディレクトリ以下を全部探索する

-r オプション 但し、無限にディレクトリ構造を掘ってしまうので実行場所によっては全然結果が返ってこなくなるので注意

grep [探索文字列] -r ./

上記が重すぎるので階層を限定したりファイル名で絞り込みたい場合

拡張子だけでいい場合

--include *.[拡張子]

階層など細かく指定したい場合

find/xargsと組み合わせる
findで対象のファイル名を取って、xargsコマンドでgrepに流し込む

maxdepthは1=>掘らない 2=>1階層下まで探索する

find -name [ファイル名パターン] -maxdepth [探索階層] -type f | xargs grep [検索文字列]

findの -type fはファイル限定。

大文字小文字を区別しない

iオプション

grep [探索文字列] -i 

gzip化されたファイルを検索する

ログローテートされた古い奴を探索する場合はzgrepコマンド

なお、そのまま読みたい場合はzcatなど、他にもz+コマンド系のコマンドが存在する。

周辺 n行を取得する

-[数値]オプション。
特定のキーワードを元に周辺のログを引っ張りたい場合など。
前後の場合はそれぞれ-B [数値] -A [数値]

grep hoge fuga.log -3 //対象行+周囲3行を取得する
grep hoge fuga.log -B 3 //対象行+前3行を取得する
grep hoge fuga.log -A 3 //対象行+後3行を取得する

6
5
0

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?