Search recursively for files named string.xml for specific text

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

  grep -r "text" * 

How can I specify a search only in files named 'strings.xml'?

+6
source share
4 answers

You want to use find for this, since grep will not work as recursively (as far as I know). Something like this should work:

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

The find searches the current directory ( . ) For a file named strings.xml ( -name "strings.xml" ), and then runs the specified grep for each file found. Curly braces ( "{}" ) are placeholders that find uses to indicate the name of a found file. More information can be found in man find .

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

+15
source

You can use the grep command:

 grep -r "text" /path/to/dir/strings.xml 
+2
source

grep supports the --include option, the use of which is to recursive in directories only the search for a file matching PATTERN. So try something like:

 grep -R --include 'strings.xml' text . 

I also tried using find, which seems pretty fast than grep:

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

These links are talking about the same problem, they can help you:

'grep -R string * .txt', even if top dir does not have a .txt file

http://www.linuxquestions.org/questions/linux-newbie-8/run-grep-only-on-certain-files-using-wildcard-919822/

+1
source

Try the command below

 find . -type f | xargs grep "strings\.xml" 

This will run grep "strings\.xml" for each file returned by find

0
source

Source: https://habr.com/ru/post/946529/


All Articles