カーネルなど大規模なソースコードを検索したい場合、findとgrepを使ったり、タグジャンプを使うだろう。Eclipse使う場合とか異論はあると思うけど、今日はfindとgrepの合わせ技について調べてみた。 普段はxargsを噛ませて、 $ find . -name \*.c | xargs grep hogehogeとかやるんだけど、findの-execオプションを使った場合とどれくらい性能差があるのだろうか? 評価環境はVMWare Fusion上のUbuntu 11.04。Linux kernel 2.6.28.10のfs以下で、まずは次の3パターンを比較してみた。3回測定してtimeの最良値を示す。おそらく対象ファイルはファイルキャッシュに載っている思う。 $ time find . -name \*.c -exec grep wait_event_interruptible {
拡張子を指定して、再帰的にgrepしたい。色々やってみた。 こんな感じ? そんなわけで、findを使って検索範囲を絞りこむ。 xargsを使うと、 % find ../ -name '*.c' | xargs grep hogeうは、めんどい。(速度は早い) コマンド置換で。 % grep hoge $(find ../ -name '*.c')ちょっとすっきり。 zshで。 % grep hoge ../**/*.cいいね。 manを真面目に読んでみるかなぁと 数年の時が流れ manを真面目に読んだら、 % grep hoge --include='*.c' -rあるじゃん。 参考 Man page of GREP 更新履歴 2013/04/11 manを真面目に読んだ。
意外と知られていないようだが、"find"はLinuxに含まれている珠玉のツールだ。findでは、名前やファイルサイズ、更新日など多数の条件でファイルを検索することができる。findを用いることで、今まで時間をかけて手作業で探さなければならなかったファイルを、簡単に検索できるようになるだろう。 最も単純な使い方から説明しよう。findを用いてシステム内にあるすべてのファイルを表示するには、以下のように入力する。 $ find / このように入力すると、すべてのファイルの一覧が表示される。特定のファイル名で検索したい場合には、次のように行う。 $ find / -name passwd /usr/bin/passwd /usr/sbin/passwd ... また、passwdという名前で、かつパーミッションが0700のファイルを検索したい場合は、次のように入力すればよい。 $ find /
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く