For multiple files (grep -F is the same as fgrep, you should use one of these)
This runs five searches at a time in parallel. So it is approximately 5 times faster than a single thread of grep, on modern multi-core cpus.
cnt=0;
for i in catalina.out*
do
cnt=$(( $cnt + 1 ))
grep -F 'xxxxxx' $i >> backup.txt
[ $(( $cnt % 5 )) -eq 0 ] && wait
done
wait
Hi Jim,
Am I correct in assuming that you intended to have an & after backup.txt in this for loop?
Should the number of grep's to be run in parallel be tied to the number of CPU cores available to the process, or is grep I/O limited?
---------------------------
Added later: Are you sure that the output from grep is line-buffered? If not you could end up with output with parts of lines intermixed by running multiple greps writing to a single output file!
You could try using the --mmap flag and process all files with 1 grep call (as there are only 20 - 30 files the command line length should be OK). This assumes more modern, GNU greps that support the mmap(2) system call to read files and that files don't shrink while grep is working: