Grep From Files and Display the File Name

by on September 13, 2010 · 13 comments· LAST UPDATED February 18, 2011


How do I grep from a number of files and display the file name only?

When there is more than one file to search it will display file name by default:

grep "word" filename
grep root /etc/*

Sample outputs:

/etc/bash.bashrc:       See "man sudo_root" for details.
/etc/crontab:17 *       * * *   root    cd / && run-parts --report /etc/cron.hourly
/etc/crontab:25 6       * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
/etc/crontab:47 6       * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
/etc/crontab:52 6       1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
grep: /etc/gshadow: Permission denied
/etc/logrotate.conf:    create 0664 root utmp
/etc/logrotate.conf:    create 0660 root utmp

The first name is file name (e.g., /etc/crontab, /etc/group). The -l option will only print filename if th

grep -l "string" filename
grep -l root /etc/*

Sample outputs:

grep: /etc/at.deny: Permission denied

You can suppress normal output; instead print the name of each input file from which no output would normally have been printed:

grep -L "word" filename
grep -L root /etc/*

Sample outputs:

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 13 comments… read them below or add one }

1 Josh Keife September 30, 2010 at 5:35 pm

for i in `ls` ; do
grep -q “whatever” $i && echo $i


2 nixCraft October 1, 2010 at 5:42 am

Why ls? It will fail when you have file names with white spaces. A better way is to use wild cards:

for i in * ; do
grep -q “whatever” $i && echo $i


3 lUser February 20, 2011 at 8:19 pm

what about
grep -rI “whatever” ./ | cut -d: -f1 | sort -u

nb it will fail on file containing “:” such as some perl man pages but…

…Or just add some proper form
for i in `ls` ; do
grep -q “whatever” “$i” && echo “$i”


4 Josh Keife September 30, 2010 at 5:41 pm

or if you need to search all file and subdirs within a dir you can run the commands above. Just replace `ls` with `find . -type f`.


5 mlathe October 2, 2010 at 12:45 am

Or better yet, use xargs.
find . -type f | xargs grep html

If you want to deal with spaces etc…
find . -type f | xargs -I {} grep -H foo "{}"


6 yoander (sedlav) October 13, 2010 at 5:36 pm

Dealing with space with a shorter form
find . -type f -print0 | xargs -0 grep foo


7 layman October 19, 2010 at 7:46 am

I agree, find is working great for me.

find -name *.log -uid 1000

You can find files only owned by uid. Nifty feature.


8 Becky Gawat March 19, 2012 at 6:55 pm

How do I grep if i am looking for a specfic percentage?
grep -w “68%-100% /usr/local/stage”

thanks in advance


9 willywonker May 3, 2012 at 8:44 pm

ls | while read FILE; do echo “$FILE” done;

This handles whitespace and other nasties nicely.


10 avinash June 7, 2012 at 5:22 pm

how can i grep
from filename AUZ_CA_EL.txt


11 BrianBlaze November 27, 2012 at 5:09 pm

cat AUZ_CA_EL.txt | grep “CA”


12 Özzesh December 5, 2012 at 7:45 am

how do I know the location and file name form the result that grep shows?
I use command like cat * | grep
Now what I need is the location of the file name along withe the grep result.

Help anyone


13 a January 11, 2014 at 10:54 pm

THX for the “$FILE” solution! Worther than 100 webpages!


Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: