简体   繁体   中英

Bash : Piping Find into Grep

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?

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM