linux - [Solved-3 Solutions] How to use grep to show just filenames on Linux ? - ubuntu - red hat - debian - linux server - linux pc
Linux - Problem :
How to use grep to show just filenames on Linux ?
Linux - Solution 1:
The standard option grep -l
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.
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 2:
From the 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.)
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 3:
For a simple file search you could use grep's -l and -r options:
grep -rl "mystring"
click below button to copy the code. By - Linux tutorial - team
All the search is done by grep. Of course, if you need to select files on some other parameter, find is the correct solution:
find . -iname "*.php" -execdir grep -l "mystring" {} +
click below button to copy the code. By - Linux tutorial - team
The execdir option builds each grep command per each directory, and concatenates filenames into only one command (+).