Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have the following command

find /var -type f -exec grep "param1" {} \; -print

With this command I can find the param1 string in any file under /var but the time that it take for this is very long.

I need another possibility to find string in file but much more faster then my example

share|improve this question
    
@yael: How long is "very long?" A minute, an hour, a day? Maybe you can help us answer your earlier ambiguous questions before we start on this one, yes? –  Mike Fitzpatrick Jun 3 '10 at 11:59
    
Please stop posting the same question on both StackOverflow and SuperUser. –  Dennis Williamson Jun 3 '10 at 15:15
add comment

5 Answers

You could try using grep directly as in

grep -R "param1" /var/*
share|improve this answer
2  
or even grep -R "param1" /var –  Kevin M Jun 3 '10 at 11:55
    
+1 definitely the way to go, and you can also use the -l option to print only the names of the matching files rather than the actual matching lines within the files. –  David Z Jun 3 '10 at 17:36
add comment

fast search is mostly achieved by having an "index" of the content, kind of a "lookup table". there are exist some free search engines which build an index of input files and give you very, very fast results:

i doubt that you want to go that way, but just to give you another option :)

share|improve this answer
    
indexed content is normally done only for stuff that isn't expected to change significantly. And by "significantly", I mean adding hundreds of lines every time it boots, such as files under /var. –  Kevin M Jun 3 '10 at 11:59
    
@Kevin M: "i doubt that you want to go that way" was intentionally put there. shall i put the drawback of the indexed approach into the answer? –  akira Jun 3 '10 at 12:10
add comment

There are a a couple factors at work here:

First, by using -exec, find is spawning a new process for grep for every file it finds.

This can be managed either by using xargs as in Felipe Alvarez's answer (I wrote a blog post about this 5+ years ago) or by using -exec grep param1 {} + (note the + instead of \;). When using +, "the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files" (from find man page)

Second, the number and size of files under /var is potentially very large.

Can you add parameters in addition to -type f to limit the files you're searching. find has options to limit by time, owner, name pattern, etc. For example, you could do:

find /var -type f -name "access*log" -mtime -7 -exec grep param1 {} +

That will find files modified in the last seven days with names starting with "access" and ending with "log". Only those matching files will be part of the exec/grep.

share|improve this answer
add comment

find ...[blah]..... -print0 | xargs -0 grep -e "regex"

share|improve this answer
add comment

I advise to use:

grep -RInis "param1" /var

For more details see

man grep

If speed is of such importance, you might want to consider fgrep, which might not prove to be faster depending on the implementation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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