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/*
zaxxon
December 30, 2009, 8:47am
3
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.
xoops
December 30, 2009, 8:55am
4
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/*
methyl
December 30, 2009, 9:15am
6
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
thegeek
December 30, 2009, 10:12pm
7
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
xoops
December 31, 2009, 4:36am
9
Why to use 2 commands when only 1 can do the job ?
grep -Rl pattern /path/to/Mydirectory/*
methyl
January 1, 2010, 12:29pm
10
@xoops
"grep -R" is not valid on Solaris. I guess you have Linux not unix?
xoops
January 4, 2010, 10:56am
11
methyl,
you are correct.
There is no "-R" with grep on solaris.
Scott
January 4, 2010, 11:06am
12
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
Thanks!