LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 10-05-2010, 06:38 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 507

Rep: Reputation: 31
How to find a string in files under the current directory?


Assumed y directory structure looks as follow:

Code:
--root
  -- dir1
     -- dir1-1
  -- dir2
     -- dir2-1
        -- dir2-2
And under each sub dir there are some log files ended with .log.

Now I know how to list all these log files with:
Code:
find . -type f -name "*\.log"
Assumed now there is a string called "Hello World" inside some of these files. How can I find out these files? I try followings but failed

Code:
 grep "This is" | find . -mtime 0 -type f -name "*\.log"
 
Old 10-05-2010, 06:43 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Try this:
Code:
find . -type f -name "*\.log" -exec grep -l 'This is' {} \;
thomas, the last 3 threads you created are basically just taking the exact same idea a little bit further each time; i.e. one thread could have addressed the last three, and using the very same command. Please try to keep such similar questions together!

Cheers!
 
1 members found this post helpful.
Old 10-05-2010, 06:45 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

You can use the -exec option that comes with find:

find . -type f -name "*\.log" -exec grep -l "Hello World" {} \;

Hope this helps.
 
1 members found this post helpful.
Old 10-05-2010, 07:32 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
With using find
Code:
grep -R whatever_you_want_to_find *
 
Old 10-05-2010, 07:41 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 507

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Wim Sturkenboom View Post
With using find
Code:
grep -R whatever_you_want_to_find *
this takes too long.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
move all files in current directory into a subdirectory in the current directory jakykong Linux - Newbie 8 07-17-2013 12:46 AM
[SOLVED] How to find files under the current directory? thomas2004ch Linux - Newbie 2 10-05-2010 06:19 AM
Find files in current directory, not subdirectories SmurfGGM Linux - Newbie 1 10-09-2008 01:14 PM
how to find files in current directory only (not recursively) babu198649 Linux - Newbie 2 12-27-2007 02:30 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 09:19 PM


All times are GMT -5. The time now is 06:44 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration