LinuxQuestions.org
Visit Jeremy's Blog.
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 04-21-2010, 06:19 AM   #1
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Rep: Reputation: 15
How to search text in all text files of all the sub-directories ?


Hi !

Currently, when I'm searching text in files of my PHP project, I use this line :
Code:
grep -r 'myTextToFind' *
But now, I would like to search only in ".lang" files.
How can I do that ?

Thanks !

-Pit

Last edited by moicpit; 04-21-2010 at 12:27 PM.
 
Old 04-21-2010, 06:44 AM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,474

Rep: Reputation: 189Reputation: 189
find / -name '*.lang' | grep -r 'myTextToFind' *

assuming root(/) is your topmost directory you want to search from.
 
Old 04-21-2010, 07:30 AM   #3
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you but it's not working : it search in all the files, not only in "*.lang" files.
 
Old 04-21-2010, 07:50 AM   #4
barrie60s
LQ Newbie
 
Registered: Jun 2008
Posts: 1

Rep: Reputation: 1
grep "string to find" `find . -name "*.lang"`

The back quotes ` generate file names for file argument to grep

bs

Last edited by barrie60s; 04-21-2010 at 08:37 AM.
 
Old 04-21-2010, 08:09 AM   #5
LouRobytes
Member
 
Registered: Mar 2010
Location: Nova Scotia, Canada
Distribution: Ubuntu & Fedora
Posts: 189

Rep: Reputation: 44
Code:
find / -name '*.lang' -print | xargs grep 'string to find'
You can be more specific than "/" with the start point.

Cheers, Lou

EDIT: If you don't want to run into permission problems, execute as root.

Last edited by LouRobytes; 04-21-2010 at 08:13 AM.
 
Old 04-21-2010, 08:19 AM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
One could also use the exec option of find,
Code:
find / -type f -name '*.lang' -exec grep 'string to find' '{}' \;
which should work equally to any other command where find generates the list on which grep is run. The empty curly parenthesis means the result of find's work (i.e. filelist), and it usually has to be enclosed in quotation marks to prevent misinterpretations. The semicolon ends the command, and that too usually has to be escaped. You can also use a plus sign instead of a semicolon at the end, and it affects the behaviour, as told by the man page of find:

Quote:
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.
You don't need to necessarily give the 'type -f', but I'm accustomed to telling find whether it should work on files only or both files and directories...

If you need to get the filenames where the matches were found as well, use the -H option for grep. Also, '*.lang' matches any file whose suffix is 'lang', but you only need to specify the dot if you (might) have files that end with 'lang' (i.e. 'thislang', 'thatlang' and so on, but not 'thislang.xxx' because it doesn't end with 'lang').

Last edited by b0uncer; 04-21-2010 at 08:28 AM.
 
Old 04-21-2010, 08:23 AM   #7
LouRobytes
Member
 
Registered: Mar 2010
Location: Nova Scotia, Canada
Distribution: Ubuntu & Fedora
Posts: 189

Rep: Reputation: 44
Thanks bOuncer.

Lou
 
Old 04-21-2010, 09:45 AM   #8
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
OK, thanks for all this !

That helped me to have this command that does exactly what I wanted :
Code:
grep -H "nb" `find . -name "*.lang"`
 
  


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 text in any files zodehala Linux - Newbie 6 11-22-2008 10:27 AM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 05:07 AM
Search text in files remotely anjanesh Red Hat 2 08-15-2005 10:03 AM
replace text in files and directories rincewind Linux - Software 4 10-27-2004 12:29 PM
Search all text files? nextekcarl Linux - General 4 10-20-2004 01:18 PM


All times are GMT -5. The time now is 12:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration