LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   fastest way to search for strings (https://www.linuxquestions.org/questions/linux-newbie-8/fastest-way-to-search-for-strings-785638/)

niteshadw 01-29-2010 11:51 AM

fastest way to search for strings
 
Is there a faster way to search for a file containing a given string than using grep -re "string" /

This takes a long time to search through the entire system, so I was wondering if there is a faster way. I don't know the name of the file, just that it will contain a given string.

Thanks!

PTrenholme 01-29-2010 01:45 PM

Look at the find command (info find) and the locate command (info locate). The locate is faster since it uses the data base of file names and locations so it doesn't have to read the drive for file names. On the other hand, find lets you filter the files by type, so you could limit the searching to files that, for example, are not executable or that were in system directories.

In fact, running grep -ar <expession> / may cause an infinite wait since it will eventually search /dev/null or /dev/random, and those (pseudo) files are infinitely long.

niteshadw 01-29-2010 06:39 PM

Quote:

Originally Posted by PTrenholme (Post 3845241)
Look at the find command (info find) and the locate command (info locate). The locate is faster since it uses the data base of file names and locations so it doesn't have to read the drive for file names. On the other hand, find lets you filter the files by type, so you could limit the searching to files that, for example, are not executable or that were in system directories.

In fact, running grep -ar <expession> / may cause an infinite wait since it will eventually search /dev/null or /dev/random, and those (pseudo) files are infinitely long.

Thanks for the reply, but as far as I know, locate and find only search by file name, not the contents of the file? I don't know the name of the file or type, just that it contains a certain string on the inside...

worm5252 01-29-2010 10:08 PM

grep searches the contents of a file. Try typing man grep

Quote:

NAME
grep, egrep, fgrep, rgrep - print lines matching a pattern

dv502 01-30-2010 06:24 AM

There is a program called Ack and it's design to do fast searches on large content files.

I heard about this program on http://fosscasts.com/screencasts

Scroll down to episode #15 Power Searching with Ack and and watch the video tutorial on its usages.

-Cheers


All times are GMT -5. The time now is 09:23 AM.