List only file names containing string in Linux

I want to specify only the names of the files containing the string using Linux commands.

When using egrep, all line instances are displayed, which can be multiple times.

For instance:

egrep disco music_file*.txt 

Show

 music_file1.txt: blah blah blah disco stuff music_file1.txt: disco blah blah blah music_file1.txt: I like to listen to italodisco music_file2.txt: I like to party to disco music_file3.txt: Does your dog like disco? 

While all I want is:

 music_file1.txt music_file2.txt music_file3.txt 

Question: How can I just show one instance of each file name when searching for a string in Linux?

+4
source share
2 answers

Add -l to the egrep expression:

 egrep -l disco music_file*.txt 

From man grep :

-l, --files-with-matches

Suppress normal output; instead, type the name of each input file from which you typically printed. Scanning will stop in the first match. (-l specified by POSIX.)

+6
source

grep -l should work for you.

  grep -l pattern files*.txt 

From the man page:

  -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.) 
+2
source

Source: https://habr.com/ru/post/1501263/


All Articles