How can i find a word inside a file from a directory and it's subdirectory ?

Suppose i have a word "mail".
I have to search this word in all files inside a directory and it's sub-directories.

It will also search in all hidden directory and sub-directories.

If it finds this word in any file it will list that file.

How can i do this with perl/ruby/awk/sed/bash or using any command ?

find /directory -type f -exec grep -l mail {} \;

Or the more efficient:

find /directory -type f -exec grep -l mail {} +

@jlliagre , @jim mcnamara,

Is it possible to get the same result with only grep command ?

---------- Post updated at 07:11 PM ---------- Previous update was at 07:05 PM ----------

I tried that , but it did not print any filename .

But if i do

grep -lri "mail" .

It prints some filename.

Actually what i'm trying to do is to print those filename which file content the word "mail". That means if i do

cat filename | grep mail

I will get some lines.

Here's what you want:

find ./ -type f -print0 | xargs -0 grep -n "mail"

This code shows the file name and the line number where "mail" is found.
Output:

/home/unixuser/.gnupg/gpg.conf:102:# Example email keyserver:
/home/unixuser/.gnupg/gpg.conf:103:#      mailto:pgp-public-keys@keys.pgp.net
/home/unixuser/.gnupg/gpg.conf:129:#keyserver mailto:pgp-public-keys@keys.nl.pgp.net
/home/unixuser/.gnupg/gpg.conf:201:# photo-viewer "metamail -q -d -b -c %T -s 'KeyID 0x%k' -f GnuPG"

and if you want to match with just the word "mail", use this:

find ./ -type f -print0 | xargs -0 grep -n " mail "

Note the spaces before and after mail within quotes. For case-insensitive search, add -i with grep:

find ./ -type f -print0 | xargs -0 grep -in " mail "

Check if your grep supports -r option, if so you can do it this way

grep -rli mail *

--ahamed

Hmm, that doesn't make sense. Try:

find . -type f -exec grep mail {} +

This does pretty much the same thing, just a simpler and cleaner way, as the convoluted alternative:

find ./ -type f -print0 | xargs -0 grep ...

Cool, the first time to see "+" used in find command.

It will print the filename not the contents of the file. It looks this find command prints the content of the file too.
Like

grep -lri "mail" *

How can i find those filename with the extension .php only ?

Huh ? It definitely prints contents of the file too. Did you try it ?

That way:

find . -name "*.php" -exec grep -i mail {} +