Basically searching all files in a directory and locating those that contain a certain word within them. For example, I want to know what files contain the word 'apple', if there is just print the filenames that contain 'apple' somewhere in the text. The only thing I really need to know is how to put a command such as find into a conditional statement, or if there are better alternatives. Thanks.

Recommended Answers

All 6 Replies

Basically searching all files in a directory and locating those that contain a certain word within them. For example, I want to know what files contain the word 'apple', if there is just print the filenames that contain 'apple' somewhere in the text. The only thing I really need to know is how to put a command such as find into a conditional statement, or if there are better alternatives. Thanks.

$ find mydir -name "*.ext" -exec grep -q "DEBUG" '{}' \; -print
    # (finds all files with .ext containg the text "DEBUG")
$ find mydir -iname "*.ext" -exec grep -q -i "debug" '{}' \; -print
    # (as above, but case insensitive filenames and pattern match)

"mydir" can obviously be any directory you want, but the find command has to have a directory name, often it would be "." for the current directory.
You can skip the -name "*.ext" if you want, this is just to filter the list of files.
It's a recursive search, that is it descends into all the subdirectories of the directory you specified, but there are switches to control that - consult man find for more details.

You could just use grep without the find. Use grep -l -r -i

You could just use grep without the find. Use grep -l -r -i

That's quite true, there are many ways to skin a cat!

Personally, though, I usually use find in cases like this, because of its greater flexibility in selecting files - such as by date accessed, date last updated, stay within the current volume, only recurse 2 levels deep, only give me files I have permission to write to, etc.

You pays your money and you gets your choice, as they say.

commented: :) +6

You could just use grep without the find. Use grep -l -r -i

Thanks, works great for one word. To go further, I am having trouble with that I can't use this when searching for multiple strings because of the -l option. Or perhaps I'm doing something wrong, i.e. if I try:

grep -i -r -l "apple" . | grep -i -l "orange" .

It won't work as desired. When I remove the -l option it displays more than the filenames which I don't want. I looked at the man file but couldn't see anything but -l flag to show only filenames.

When I try to pipe 'find' similar to what Simon said, I can't seem to get the correct result either.

grep doesn't look for words. It looks for regular expression matches. You need to build a regular expression which matches either "apple" or "orange". For example, grep 'apple\|orange' . Different versions of grep have slightly different regexp syntax; read your manpage, and just play with them a little.

Guess your prob is solved already.. just check if you want to use "-w" with grep as you seem to be interested in matching the word.
Check out egrep if you grep doesn't support advanced stuff. Some greps support -e as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.