How to recursively find a .doc file that contains a specific word?

9

3

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?

Tom

Posted 2011-08-31T11:57:23.680

Reputation: 125

1and maybe also return the name of the file that contains the word? – Tom – 2011-08-31T12:02:09.353

Answers

11

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.)

user1686

Posted 2011-08-31T11:57:23.680

Reputation: 283 655

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 – 2011-08-31T12:09:08.100

1@user: Try the second suggestion, which, by the way, is titled "This will also output the file name". – user1686 – 2011-08-31T12:10:18.167

can probably simplify a bit: find -name \*.doc -exec sh -c "catdoc '{}' | grep -q 'specificword' && echo {}" \; – glenn jackman – 2011-08-31T14:20:40.643

5

You might want to look at recoll which is a full-text search tool for Linux and Unix systems supporting many different document formats. However, it is index-based, i.e., it has to index the documents you want to search in before the actual search. (Thanks to pabouk for pointing this out).

There is a GUI and a command line, too.

See the documentation for further infos.

Robert Jakob

Posted 2011-08-31T11:57:23.680

Reputation: 51

1Maybe it is worth to note that Recoll provides indexed search. First it has to index the documents then it can search through the indexes. – pabouk – 2013-12-17T08:25:38.633

1

Grep should find binary matches with:

find /path/to/dir -name '*.doc' exec grep -l "specificword" {} \;

Xenoactive

Posted 2011-08-31T11:57:23.680

Reputation: 992