tags:

views:

71

answers:

5

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 other possibility to find string in file but much more faster then my example

THX

yael

+2  A: 
grep -r "string"

The find is not neccesary.

This is a good link, though outdated.

link text

Also i think this belongs in superuser.com

DRL
+2  A: 
find /var -type f  | xargs grep "param1" 

would be slightly faster (no process spawning for each file)

grep -r "param1" /var 

would be slightly more so I think.

Noufal Ibrahim
but if the files are "too much", this could cause problems. Moreover, better to use `-print0` and `-0` accordingly, and add the `-n` too to prevent preview possible problem.
ShinTakezou
+1  A: 

Try also using ack, which is "better than grep" in most cases. Among its features the ability to ignore typical garbage files by default (such as .svn or .git directories, core dumps, backup files), the ability to use a large set of predefined file classes, nice output formatting.

UncleZeiv
where I can download the ack?
yael
@yael: Install it using your package manager (preferred) or download it at the link that **UncleZeiv** conveniently provided.
Dennis Williamson
Just be careful when installing it from your distribution as there is often another package named "ack" around, which has nothing to do with this. In Ubuntu you want to `apt-get install ack-grep`. Happy acking :)
UncleZeiv
+1  A: 

Take a look at the -l option to the grep command for a speed boost. To speed up the find command use:

find ... -exec sh -c '...' arg0 '{}' +


# grep ... -l: print files with matches, but stop scanning the file on the first match
grep -lsr "param1" /var

find /var -type f -exec sh -c 'grep -ls "param1" "$@"' arg0 '{}' +
find /var -type f -exec sh -c 'grep -ls "$0" "$@"' "param1" '{}' +
yabt
sorry but its not work find /var -type f -exec sh -c 'grep -ls "$0" "$@"' "param1" '{}' + find: missing argument to `-exec'
yael
+1  A: 

You can use locate's index (if you don't depend on files that are added/removed)

grep "param1" $(locate -r '^/var')
krico