GNU/Linux >> Linux の 問題 >  >> Linux

Linuxでgrepを使用してファイル名だけを表示するにはどうすればよいですか?

簡単なファイル検索には、grep の -l を使用できます と -r オプション:

grep -rl "mystring"

すべての検索は grep によって行われます。もちろん、他のパラメータでファイルを選択する必要がある場合は、 find が正しい解決策です:

find . -iname "*.php" -execdir grep -l "mystring" {} +

execdir オプションは、各ディレクトリごとに各 grep コマンドを構築し、ファイル名を 1 つのコマンドのみに連結します (+ ).


標準オプション grep -l (小文字の L) でこれを行うことができます。

Unix 標準から:

-l
    (The letter ell.) Write only the names of files containing selected
    lines to standard output. Pathnames are written once per file searched.
    If the standard input is searched, a pathname of (standard input) will
    be written, in the POSIX locale. In other locales, standard input may be
    replaced by something more appropriate in those locales.

-H も必要ありません


grep(1) から マニュアルページ:

  -l, --files-with-matches
          Suppress  normal  output;  instead  print the name of each input
          file from which output would normally have  been  printed.   The
          scanning  will  stop  on  the  first match.  (-l is specified by
          POSIX.)

Linux
  1. Linuxgrepコマンドの使用方法

  2. Linuxでcronを使用する方法

  3. LinuxでFINDを使用する方法

  1. LinuxでBusyBoxを使用する方法

  2. LinuxまたはFreeBSDでGrepコマンドを使用する方法

  3. FAT ファイル システムで rsync を使用するにはどうすればよいですか?

  1. Linuxのアクセシビリティ設定の使用方法

  2. LinuxでUnzipを使用する方法

  3. LinuxでSuコマンドを使用する方法