I want to list just the names of files that contain a string using Linux commands.
When using egrep, all the instances of the string are shown, which could be multiples times.
For example:
egrep disco music_file*.txt
Might 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?
Whereas 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?
Add -l
to your egrep expression:
egrep -l disco music_file*.txt
From man grep
:
-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.)
grep -l should work for you.
grep -l pattern files*.txt
From 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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With