LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Thorough search for a text file with a specific keyword inside (https://www.linuxquestions.org/questions/linux-general-1/thorough-search-for-a-text-file-with-a-specific-keyword-inside-4175480401/)

educateme 10-11-2013 05:12 AM

Thorough search for a text file with a specific keyword inside
 
I seem to have lost a specific file of mine. I forgot its name and the directory it is in, but I know what is inside. I know that (1) the filename ends in ".txt" and (2) I know a unique keyword that I am sure is inside this text file (say, "mykeyword"). Armed with these two facts, what is the best/fastest linux command that I can use to search my entire disk for this specific file?

colucix 10-11-2013 05:30 AM

Code:

locate -0 '*.txt' | xargs -0 grep mykeyword

TenTenths 10-11-2013 06:03 AM

Code:

find / -name "*.txt" -exec grep mykeyword {} \;
while slower, doesn't rely on the "locate" database being up-to-date

educateme 10-11-2013 08:08 AM

Thanks for the replies. I was not successful with the locate command. I might have an obsolete database.

Code:

find / -name "*.txt" -exec grep mykeyword {} \;
was semi-successful: it managed to display the line in which "mykeyword" was in the file I was looking for. But unfortunately the filename nor the path to this file was not displayed, just the single line inside the file. So I unfortunately still can't locate the file.

TenTenths 10-11-2013 08:22 AM

Quote:

Originally Posted by educateme (Post 5043959)
Thanks for the replies. I was not successful with the locate command. I might have an obsolete database.

Code:

locate -0 '*.txt' | xargs -0 grep mykeyword
was semi-successful: it managed to display the line in which "mykeyword" was in the file I was looking for. But unfortunately the filename nor the path to this file was not displayed, just the single line inside the file. So I unfortunately still can't locate the file.

try:

Code:

grep -H mykeyword
-H tells grep to give the filename

educateme 10-11-2013 09:32 AM

Splendid!
Code:

find / -name "*.txt" -exec grep -H mykeyword {} \;
works. Thanks.

TenTenths 10-11-2013 09:34 AM

Quote:

Originally Posted by educateme (Post 5044009)
Splendid!
Code:

find / -name "*.txt" -exec grep -H mykeyword {} \;
works. Thanks.

You're welcome, feel free to mark my post as helpful and mark this thread as solved ;) ;)


All times are GMT -5. The time now is 10:46 AM.