[grep] Print only the names of files with a match

Created on November 30, 2026

Use grep -l to output the name of each matching file once. The example searches three files for ERROR.

printf 'INFO start\nERROR failed\n' > app.log
printf 'OK\n' > other.log
printf 'ERROR timeout\n' > archive.log

grep -l 'ERROR' app.log other.log archive.log
app.log
archive.log

-l is short for --files-with-matches. grep stops reading a file once it finds the first match.

Reference

Shell command grep