grep -n -C2
will give the line number and two lines of context
Main Topics
Browse All Topicshow do i recursively grep (search text files) in unix?
for example, if i want to find the word "example" in a unix directory and all its subdirectories? i want the file name, file path, and line number with context would be nice too.
I am in a bash shell
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
thank you.
I tried -R but didn't work
when I tried the find command, hundreds of lines flew by.
I tried the -C2 command, but it did nothing. I guess that's not the command and it was waiting for more input.
I am on redhat enterprise 2, and suse 10.
how do i know if my unix supports -R for grep other than finding it does not work. I looked in info, and it mentioned it in the documenation, but maybe documentation is not specific to your unix.
cd to the parent (top level) directory you want to search from.
Try this little loop on the command line: After the first line, you'll get > prompts to complete it
find . -type f -exec grep -l example {} \; | while read file
do
echo "-- found in $file ---"
echo "grep -2 example $file"
echo "\n"
done
If needed you can pipe it to something like less, more a file etc.:
done | less
or
done | more
or
done >> myfile
man grep produces a file that's 300 lines long. do you want all the lines?
NAME
grep, egrep, fgrep - print lines matching a pattern
SYNOPSIS
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
DESCRIPTION
Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for
lines containing a match to the given PATTERN. By default, grep prints the matching lines.
In addition, two variant programs egrep and fgrep are available. Egrep is the same as grep -E. Fgrep is the
same as grep -F.
OPTIONS
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous
groups of matches.
-a, --text
Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
this is bizarre
if i
grep octopus *.*
in my home directory which contains test.txt which says "octopus"
i get
test.txt:octopus
which is correct
however, if i go to a subdirectory which has the word octopus in that directory (in a text file) and in a subdirectory
and do
grep octopus *.*
then i only get the answer
octopus
and nothing about where it appears
if I do grep -r octopus *.*
same answer
octopus
grep will only display a filename if there are two or more files being searched.
In your home directory, you will have more than one file, whereas in the subdirectory, you'll have a single file.
If you want to ensure you always have the filename reported, use the -H flag, eg:
grep -H octopus *
or on systems that don't have GNU grep, you can acheive the same by doing
grep octopus * /dev/null
Specifying a dummy file (/dev/null in this instance) forces grep to report the filename as it is effectively searching >1 files.
Business Accounts
Answer for Membership
by: ozoPosted on 2007-05-16 at 13:46:35ID: 19104155
grep -R
or if your grep doesn't support -R
find . -type f -print | xargs grep example