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
 
LinkBack Search this Thread
Old 12-21-2011, 09:40 AM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 74

Rep: Reputation: 15
run grep only on certain files using wildcard


I am trying to grep a string in recursive directories, but I want to search only xml files.

I tried this command, which does not return any results

grep -R mystring *.xml

However, if I run this command

grep -R mystring *

I get a bunch of xml, log and properties files containing the string mystring.

How do I search recursively only in xml files?

I apologise if this has already been answered elsewhere, but I did click on the link to view similar threads. Maybe my subject wording is not good enough.

Thanks in advance
 
Old 12-21-2011, 10:08 AM   #2
alanchavez
LQ Newbie
 
Registered: Dec 2011
Posts: 1

Rep: Reputation: Disabled
Here is a one-liner,
I set up a testing directory in /home/temp/another/level/to/find with several xml , jpg, gif, and xls files and this was the output I got:

find /home/ -name "*.xml" -type f -exec grep -H "find me" {} \;

/home/temp/another/level/to/find/gato.xml:find me
/home/temp/another/level/to/find/jirafa.xml:find me
/home/temp/another/level/to/find/leon.xml:find me
/home/temp/another/level/to/find/perro.xml:find me

Last edited by alanchavez; 12-21-2011 at 10:20 AM.
 
Old 12-21-2011, 10:12 AM   #3
DrPete
LQ Newbie
 
Registered: Sep 2011
Location: UK
Distribution: Gentoo
Posts: 2

Rep: Reputation: Disabled
grep supports an --include option - for what you want to do, you would use:

grep -R --include="*.xml" mystring *
 
1 members found this post helpful.
Old 12-21-2011, 10:33 AM   #4
impert
Member
 
Registered: Feb 2009
Posts: 275

Rep: Reputation: 53
Code:
ls -aR |grep .xml
 
Old 12-21-2011, 12:54 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
grep only recurses on directory names passed to it. Globs expand into lists of matching names in the present working directory, so * means you're passing it all files and directories in the pwd, and it recurses on the directory names. But *.xml only expands into a list of the xml files in the pwd, and so there's nothing for it to recurse on.

So you have to make sure the top-level directories you want it to search are passed to it as arguments, and use --include/--exclude to control the matching of the filenames, as shown above. Or else use a tool like find to generate the filelist first.

And read the grep info page, which has more details about its operation than the man page.

@impert: In general, ls should not be be used for generating lists of filenames. Most of the time you'll do better to use shell globbing for simple matches, and find for more complex ones (and use the null separator option).

Last edited by David the H.; 12-21-2011 at 12:56 PM.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep regex with wildcard yknot7 Linux - Newbie 6 11-10-2011 01:11 AM
copy wildcard files from directories to matching ones in another dir akufoo Linux - Newbie 1 02-11-2011 11:56 PM
using curl to DL files from HTTP sites with wildcard dehuszar Programming 7 09-03-2009 04:52 AM
how to unzip all files with a wildcard path? adamrosspayne Linux - Newbie 12 06-20-2006 01:16 PM
Why doesn't a wildcard chmod change "dot" files/directories? jht2k Linux - General 1 08-09-2004 02:31 PM


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

Main Menu
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