grep all files in a directory

I am not sure if i am doing this correctly since it returns quickly. i need to grep for a keyword in all files in a directory

grep keyword /mydirectory

is that correct? I just want to know which files have a keyword in it.

i am using korn shell in solaris 5.1. There does not appear to be a -r option in this version.

grep -r "keyword" /path/pathdir
grep: illegal option -- r
Usage: grep -hblcnsviw pattern file . . .

Try:

grep "keyword" /mydirectory/*

grep will take file(s) as parameter, not directories. So try

grep keyword /mydirectory/*

To search recursively you can add -R to some versions of grep. I often use on the command line something like

find .| grep pattern

If you don't get output in which file the pattern was found, you can add maybe a -l to your grep.

You can try this..

for all files in current directory

grep Keyword * 

for all files in current and subdirectories

grep -R Keyword * 

if you want to list only filenames

 
grep -l -R keyword * 

use -l (form look) option:-

grep -l "pattern" /path/to/Mydirectory/*

Or (if you don't mind searching subdirectories but do want to avoid searching directory files):

find /mydirectory/ -type f -print | while read FILENAME
do
       grep -l "keyword" "${FILENAME}"
done

But why does that requires a loop,

find /mydirectory/ -type f -exec grep -l 'keyword' {} \;

Or even faster

find /mydirectory/ -type f | xargs grep -l 'Keyword'

:D:D:D

Why to use 2 commands when only 1 can do the job ?

 
grep -Rl pattern /path/to/Mydirectory/*

@xoops
"grep -R" is not valid on Solaris. I guess you have Linux not unix?

methyl,
you are correct.
There is no "-R" with grep on solaris.

The OP said Solaris 5.1. Does he mean SunOS 5.1 (as in Solaris 2.1), or was SunOs 5.10 (as in Solaris 10) meant (in which case he'd have /usr/sfw/bin/ggrep, which does have -R)?

I didn't know about the /usr/sfw/bin directory on Solaris 10 :slight_smile:
Thanks!