Skip to content
July 17, 2012 / doganay

RECURSIVE GREP

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

You can simply use one command find:

find . -type f | xargs grep "SEARCH_PATTERN" *

or if you want a better formatted text, here is my script:

##Written by Mustafa DOGANAY
export PATTERN="SEARCH_PATTERN"
export DIR="/absolute_search_path"
echo "" > result.txt
find $DIR -type f > files.txt 2>/dev/null
for i in `cat files.txt`
do
grep "$PATTERN" $i > tmp1
if [ -s tmp1 ]
then
echo $i >> result.txt
cat tmp1 >> result.txt
echo "" >> result.txt
echo "" >> result.txt
fi
done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: