LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-11-2013, 05:12 AM   #1
educateme
LQ Newbie
 
Registered: Jul 2012
Posts: 12

Rep: Reputation: Disabled
Post 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?
 
Old 10-11-2013, 05:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
locate -0 '*.txt' | xargs -0 grep mykeyword
 
Old 10-11-2013, 06:03 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Code:
find / -name "*.txt" -exec grep mykeyword {} \;
while slower, doesn't rely on the "locate" database being up-to-date
 
1 members found this post helpful.
Old 10-11-2013, 08:08 AM   #4
educateme
LQ Newbie
 
Registered: Jul 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.

Last edited by educateme; 10-11-2013 at 09:33 AM.
 
Old 10-11-2013, 08:22 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by educateme View Post
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
 
Old 10-11-2013, 09:32 AM   #6
educateme
LQ Newbie
 
Registered: Jul 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Splendid!
Code:
find / -name "*.txt" -exec grep -H mykeyword {} \;
works. Thanks.
 
Old 10-11-2013, 09:34 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by educateme View Post
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
 
  


Reply

Tags
find, inside, search, text



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search text inside PDF files yogomix Linux - Desktop 7 09-15-2014 05:12 AM
[SOLVED] Read the folder name from file and check a keyword in one of the file inside that richa07 Linux - Newbie 4 06-28-2013 11:06 AM
How to use AWK to search for a specific string inside html? LuxLuv Programming 4 01-28-2010 07:54 AM
Search for text inside files alaios Linux - Newbie 7 03-12-2006 09:20 AM
search for specific text in fields using awk Helene Programming 2 04-23-2004 12:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration