IT系ノウハウ Linux

ディレクトリは以下のファイル内に特定の文字列を含むファイルを検索する方法|Linux, grep コマンド

前提

以下のような二つのファイルが同じフォルダ内に存在していたとする

hellomyworld@myhost MINGW64 ~/Downloads/test
$ ls
test1.txt  test2.txt

hellomyworld@myhost MINGW64 ~/Downloads/test
$ cat test1.txt
hello world

hellomyworld@myhost MINGW64 ~/Downloads/test
$ cat test2.txt
hello hello

検索方法

大文字小文字区別する場合

world という文字列を含むファイルを検索したい場合、以下のコマンドを使う

grep "<文字列>" -rl ./

実行例
hellomyworld@myhost MINGW64 ~/Downloads/test
$ grep "world" -rl ./
./test1.txt

大文字小文字区別しない場合

大文字小文字を区別せず検索したい場合は、以下のように -i オプションを付与する

grep -i "<文字列>" -rl ./

実行例
hellomyworld@myhost MINGW64 ~/Downloads/test
$ grep "WORLD" -rl ./

hellomyworld@myhost MINGW64 ~/Downloads/test
$ grep -i "WORLD" -rl ./
./test1.txt

オプション

  • -r ディレクトリ内も対象とする
  • -l ファイル名のみ出力する
  • -i 大文字小文字の区別をしない

この記事が役に立ったという方は
ボタンをポチッとしてくれたら喜びます

-IT系ノウハウ, Linux
-, ,