LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-13-2008, 07:24 PM   #1
MJBoa
LQ Newbie
 
Registered: May 2008
Posts: 15

Rep: Reputation: 0
How to search through all files in a folder, recursively...


So I have a folder with files in it and these have files and folders which have folders with files... I have a complicated directory structure but I want to search through every file ending with a certain extension, they are text files and I want to find a certain word. What's the fastest way to do this through the command line?
 
Old 09-13-2008, 07:25 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Code:
find /path/to/folder -name "*.ext" -print0 | xargs -0 grep word
Replace ext and word appropriately.

Last edited by Mr. C.; 09-13-2008 at 09:05 PM.
 
Old 09-13-2008, 08:04 PM   #3
MJBoa
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
find `pwd`/myproject -name "*.py" -print 0 | xargs -0 grep down
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

down being the word
 
Old 09-13-2008, 08:08 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680
Use a dot instead of `pwd`.
 
Old 09-13-2008, 08:15 PM   #5
MJBoa
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Yeah that's not the problem, as indicated by the error message.
 
Old 09-13-2008, 08:29 PM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
what is `pwd` meant to be?
 
Old 09-13-2008, 08:48 PM   #7
MJBoa
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
pwd = print working directory = the current directory, it was so I didn't have to type it. I'm sure I could have omitted it but I felt like adding it.
 
Old 09-13-2008, 08:51 PM   #8
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Lose the space between print and 0
 
Old 09-13-2008, 09:06 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Oops, the typo space in between the -print and the 0 was my fault. Sorry. All fixed. Thanks for the correction billymayday.
 
Old 09-13-2008, 09:13 PM   #10
MJBoa
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Alright, thanks a lot guys.
 
Old 09-13-2008, 10:05 PM   #11
Foldarn
LQ Newbie
 
Registered: Sep 2008
Location: Northern UT
Posts: 13

Rep: Reputation: 0
What is the xargs command for? I usually just use:

Code:
find . -name "*.txt" -exec grep {} text_to_search_for \;
and that just returns the stuff I'm looking for.

Last edited by Foldarn; 09-13-2008 at 10:16 PM.
 
Old 09-14-2008, 12:12 AM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
With -exec, you are forking/execing as many processes as items found. This could be 10's or 100's or 1000's, and creates a substantially larger load on the system. Here's some proof that should convince you:

Code:
$ find . -type f | wc   
    4487    4487  152335

$ time find . -type f -exec grep -q fubar {} \;

real    0m11.974s
user    0m3.233s
sys     0m3.913s

$ time find . -type f -print0 | xargs -0  grep -q fubar 

real    0m0.091s
user    0m0.000s
sys     0m0.097s
So, that's almost 12 seconds vs. about .1 second. Your method proves to be 120 times slower.

This is on a relatively unloaded system. With a system actually performing some work, the difference will be much greater.
 
Old 09-14-2008, 10:14 AM   #13
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
.........oops!

Last edited by lwasserm; 09-14-2008 at 10:23 AM.
 
Old 09-14-2008, 11:25 PM   #14
Foldarn
LQ Newbie
 
Registered: Sep 2008
Location: Northern UT
Posts: 13

Rep: Reputation: 0
Quote:
Originally Posted by Mr. C. View Post
This is on a relatively unloaded system. With a system actually performing some work, the difference will be much greater.
Oh, snap! Thanks, that's incredible! You learn something new every day... I have a few scripts I need to rewrite using this.

Come to think of it, I hate you! You're making me do more work! *sigh* And to think tomorrow's Monday. Another reason to hate it.
 
  


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
Search a string recursively under one directory say_hi_ravi Programming 6 07-09-2008 04:47 AM
recursively search basak Linux - Software 1 08-13-2006 10:25 AM
FTP need to transfer entire folder recursively rolster Linux - Newbie 6 02-02-2006 08:54 AM
To recursively search with grep grautu Slackware 5 11-21-2005 02:53 AM
Moving certain files recursively preservice folder structure DigiTalk Linux - Newbie 11 09-08-2005 08:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:17 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