I'm using bash under Ubuntu.

Currently this works well for the current directory:

catdoc *.doc | grep "specificword" 

But I have lots of subdirectories with .doc files.

How can I search for, let's say, "specificword" recursively?

link|improve this question
and maybe also return the name of the file that contains the word? – Tom Aug 31 '11 at 12:02
feedback

2 Answers

up vote 3 down vote accepted

Use find for recursive searches:

find -name '*.doc' -exec catdoc {} + | grep "specificword"

This will also output the file name:

find -name '*.doc' | while read -r file; do
    catdoc "$file" | grep -H --label="$file" "specificword"
done

(Normally I would use find ... -print0 | while read -rd "" file, but there's maybe a .0001% chance that it would be necessary, so I stopped caring.)

link|improve this answer
Thanks grawity, the first suggestion works quit well. Is there a way to print the file name? it's only printing the phrase in which it has been found. – Tom Aug 31 '11 at 12:09
@user: Try the second suggestion, which, by the way, is titled "This will also output the file name". – grawity Aug 31 '11 at 12:10
Oh sweet :) thanks – Tom Aug 31 '11 at 12:11
can probably simplify a bit: find -name \*.doc -exec sh -c "catdoc '{}' | grep -q 'specificword' && echo {}" \; – glenn jackman Aug 31 '11 at 14:20
feedback

Grep should find binary matches with:

find /path/to/dir -name '*.doc' exec grep -l "specificword" {} \;
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.