Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively search files named string.xml for certain text

Tags:

grep

unix

This command will search all directories and subdirectories for files containing "text"

grep -r "text" *

How do i specify to search only in files that are named 'strings.xml'?

like image 922
thisiscrazy4 Avatar asked Jun 04 '13 18:06

thisiscrazy4


People also ask

How do I find all files containing specific text?

Without a doubt, grep is the best command to search a file (or files) for a specific text. By default, it returns all the lines of a file that contain a certain string. This behavior can be changed with the -l option, which instructs grep to only return the file names that contain the specified text.

How do I search for text in XML?

By using XML search syntax in text search queries, you can search XML documents for structural elements (tag names, attribute names, and attribute values) and text that is scoped by those elements. Note that plain searches do not search the attribute field in an XML document. XML query tokenization.

How do I search for a string in a directory recursively?

To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.


1 Answers

You'll want to use find for this, since grep won't work that way recursively (as far as I know). Something like this should work:

find . -name "strings.xml" -exec grep "text" "{}" \;

The find command searches starting in the current directory (.) for a file with the name strings.xml (-name "strings.xml"), and then for each found file, execute the grep command specified. The curly braces ("{}") are a placeholder that find uses to specify the name of the file it found. More detail can be found in man find.

Also note that the -r option to grep is no longer necessary, since find works recursively.

like image 62
Dan Fego Avatar answered Oct 08 '22 05:10

Dan Fego