Jak Mogę używać grepa do wyświetlania tylko nazw plików (brak dopasowań w wierszu) w Linuksie?

Jak mogę używać grep do wyświetlania tylko nazw plików (brak dopasowań w wierszu) w Linuksie?

Zazwyczaj używam czegoś takiego:

find . -iname "*php" -exec grep -H myString {} \;

Jak mogę pobrać nazwy plików (ze ścieżkami), ale bez dopasowań? Czy muszę używać xargs? Nie widziałem sposobu, aby to zrobić na mojej stronie man grep.

 784
Author: Peter Mortensen, 2011-07-10

3 answers

Standardowa opcja grep -l (czyli małe litery L) może to zrobić.

From the Unix standard :

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

Nie potrzebujesz również -H w tym przypadku.

 1184
Author: Random832,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-12 19:33:20

Ze strony man 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.)
 104
Author: Ignacio Vazquez-Abrams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-09 22:26:38

Do prostego wyszukiwania plików możesz użyć opcji grepa -l i -r:

grep -rl "mystring"

Wszystkie poszukiwania wykonuje grep. Oczywiście, jeśli trzeba wybrać pliki na innym parametrze, find jest poprawnym rozwiązaniem:

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

Opcja execdir buduje każde polecenie grep dla każdego katalogu i łączy nazwy plików tylko w jedno polecenie (+).

 24
Author: the Tin Man,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-05 00:11:58