LoginSignup
5
7

More than 5 years have passed since last update.

findとgrepでファイルから特定の文字列をリストとして取得する

Last updated at Posted at 2018-10-10

「ファイル名」から特定の文字列を取得

指定のディレクトリのみ
ls [検索するディレクトリのパス] | grep "[検索する文字列]"
指定のディレクトリとその下層まで
find [検索するディレクトリのパス] -type f -name "*[検索する文字列]*"

「ファイルの中身」から特定の文字列を取得

find [検索するファイルのパス] -type f | xargs grep -n "[検索する文字列]"
  • xargs 「標準入力やファイルからリストを読み込み、コマンドラインを作成して実行する」コマンド

追記

@h084 さんのコメントから。

find [検索するパス] -type f -exec grep -n "[検索する文字列]" {} \+

参考サイト

xargs コマンド――コマンドラインを作成して実行する
ファイルなどを検索する!findコマンドの詳細まとめ【Linuxコマンド集】

5
7
4

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