14

The following command finds all occurrences of 'some string' by recursively searching through the current directory and all sub-directories

grep -r -n  'some string' .

This command recursively searches through current directory and all sub-directories and returns all files of the form *.axvw

find . -name '*.axvw' 

I want to put these two commands together so I get all occurances of 'some string' by recursively searching through the current directory but only looking at files that end in 'axvw'.

When I tried running the following command nothing was returned:

find . -name '*js' | grep -n  'some string'

What am I doing wrong?

user2135970
  • 795
  • 2
  • 9
  • 22
  • 1
    See the answers given here: http://serverfault.com/questions/9822/recursive-text-search-with-grep-and-file-patterns – Lars Fischer Apr 27 '16 at 20:10
  • Possible duplicate of [How to use pipe within -exec in find](https://stackoverflow.com/questions/21825393/how-to-use-pipe-within-exec-in-find) – koppor Jan 31 '18 at 08:43

2 Answers2

14

You can use -exec option in find:

find . -name '*.axvw' -exec grep -n 'some string' {} +

Or else use xargs:

find . -name '*.axvw' -print0 | xargs -0 grep -n 'some string'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    `-exec grep -n 'some string' {} +` would make the `find -exec` approach competitive with xargs on performance, and is guaranteed to be available by the 2006 POSIX spec. – Charles Duffy Apr 27 '16 at 20:14
2

find . -name '*js' -exec grep -n 'some string' {} \;

Should work I think.

Edit: just for fun, you could also use a double grep I believe.

find . | grep 'some string' | grep js

sjwarner
  • 452
  • 1
  • 7
  • 20