Operating System - HP-UX
1834133 Members
2263 Online
110064 Solutions
New Discussion

easy unix searching question

 
SOLVED
Go to solution
Joe Johnson
Advisor

easy unix searching question

How do I search all text files on the server for a certain word?

Thanks in advance...
4 REPLIES 4
linuxfan
Honored Contributor

Re: easy unix searching question

Hi,

You could try something like
find /path -type f -exec grep 'string' {} \;

where path is the directory and string is the word you are looking for.

If you are looking for a word in text files which are located in the same directory, you could try something more simpler like
grep 'word' *

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
harry d brown jr
Honored Contributor
Solution

Re: easy unix searching question

Also, if you aren't sure of the case of the word use the "-i" option on grep, like this:

find /path -type f -exec grep -i "string" {} \;

If you want the filenames, you can add the "-l" (as in lower case L) to the grep command. If you suspect the file size to be less than 1Meg, you can add the "-size -1000000c" to the find command, as in:

find /path -type f -size -1000000c -exec grep -il "string" {} \;

Live Free or Die
Joe Johnson
Advisor

Re: easy unix searching question

Thanks for the responses, that is what I needed...
Leslie Chaim
Regular Advisor

Re: easy unix searching question

Hi,

The -type f to find will be true on executable file as well. This may cause some gibberish output from the grep command. To get around this create a small scrip called is_text
with the following line:

file $1 | grep -q text

then chmod your script and invoke find:

find /path -type f -exec is_text {} \; -exec grep -il {} /dev/null \;

The /dev/null is a trick to force grep to report the filename.

lzc
If life serves you lemons, make lemonade