1829147 Members
2653 Online
109986 Solutions
New Discussion

find and grep

 
SOLVED
Go to solution
Sachin_40
Occasional Contributor

find and grep

I want to grep for a particular string mystring in all the subdirectories and redirect those file names to a file and also i want to write to a file all the filenames those files which "find" could not search/read .
I am doing
(find . -name "*" -print |xargs "mystring" -print >/tmp/out.txt)>>& /tmp/err.txt
I can run it on commnad line but when I run it thru a script i am getting an error.
Syntax error at line 2 : `&' is not expected.
The reason to run it thru a script because i want to sudo to run the script so that i can read most of the directories.
Any other find type or any other solution??
btw i am trying to run it on SunOS
Appreciate your help
Thanks,
6 REPLIES 6
H.Merijn Brand (procura
Honored Contributor
Solution

Re: find and grep

Almost right, I think

# ( find . | xargs grep "mystring" >/tmp/out.txt) 2>> /tmp/err.txt

You used a csh like syntax

% ( find . | xargs grep "mystring" >/tmp/out.txt) >>& /tmp/err.txt

But GNU grep is the easiest for that

# grep -r "mystring" .

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: find and grep

You should be able to do the following-

find . -print | xargs grep -l "mystring" >/tmp/err.txt

I'm not sure why you have the "&" in the command. Usually it is used to redirect to another file number.

HTH

-- Rod Hills
There be dragons...
Stuart Abramson
Trusted Contributor

Re: find and grep

##
# hpeas01:/root/bin/search.ksh SDA 11/21/01
#
# Find the string in any text file in the search path
#
USAGE="search.ksh "
#

# Check for 1 argument:
if (( $# != 1 ))
then
print $USAGE
exit 1
fi

STRING=$@

find . -type f | while read FILE
do
file $FILE | grep -q text
if [[ $? = 0 ]] # We have a text file
then
strings $FILE | grep $STRING
if [ $? = 0 ] # I found the
then
print "==> " $FILE "\n"
fi
fi
done
harry d brown jr
Honored Contributor

Re: find and grep

In response to your biggest problem of "btw i am trying to run it on SunOS". I would suggests you use the server for a door stop :-))

To find mystring in the filename use:

find . -name "*mystring*" 1>/tmp/out.txt 2> /tmp/err.txt

To find mystring in the files use:

find . -exec grep -l "mystring" 1>/tmp/out.txt 2> /tmp/err.txt

live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: find and grep

Sorry, I screwed up on the syntax, here is the correction:

for "mystring" within files:

find . -type f -exec grep -l "mystring" {} \; 1>/tmp/out1.txt 2> /tmp/err1.txt

for "mystring" within file names:

find . -type f -name "*mystring*" 1>/tmp/out1.txt 2> /tmp/err1.txt

live free or die
harry d brown jr
Live Free or Die
Sachin_40
Occasional Contributor

Re: find and grep

thanks guys!!!!