This question already has an answer here:

Over a decade ago I imagined an improved version of grep that would use indexes to make recursive searching faster. I never got around to writing it, but I wonder if somebody else did!

My original idea was:

  • Keep a cached index of word -> [line-numbers] for each file, which would automatically update when the file size or timestamp changes.
  • Use the cache for lightning-fast searches over any files with up-to-date indexes, if the expression can make use of it.
  • For search expressions where the index cannot help, default to grep's usual behaviour.

Although this optimization would be limited to whole-word-searching, I thought it should be grep-compatible, so that it could be dropped in to replace /usr/bin/grep and help speed up various existing scripts that use grep.

There are limitations to this idea:

  1. Only word-searches or searches including certain \\<...\\> terms would be able to benefit from the indexes, and the latter case could be complex to implement.
  2. The time taken to check file stats to ensure the caches are up-to-date might make the time saved on searches minimal.

So do you think it would be worthwhile, or does point 2 make the idea worthless?

And has someone already tried this? If so, do please share.

marked as duplicate by Gilles, slm, Braiam, strugee, jasonwryan Mar 15 '14 at 2:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


This looks promising.


Code Search is a tool for indexing and then performing regular expression searches over large bodies of source code.

It does not accept the same arguments are grep, notably it does not accept a file list and -r is assumed. However it does support a few of grep's favourite flags.

usage: csearch [-c] [-f fileregexp] [-h] [-i] [-l] [-n] regexp

So it can not act as a drop-in replacement for grep, but under certain conditions a call to grep could be converted into a call to csearch.

The regexp format (RE2, nearly PCRE) also differs from GNU grep. For example the patterns \< and \> cannot be used for start/end word; but instead we can use \b for word-boundary.

To install

On Debian and Debian clones:

apt-get install codesearch

For Ubuntu 12.04 (and other versions before saucy), download it by selecting your arch (i386/amd64) here and then install it with:

$ sudo dpkg -i ~/Downloads/codesearch_*.deb

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