Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash : Piping Find into Grep

Tags:

grep

find

bash

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?

like image 409
user2135970 Avatar asked Apr 27 '16 20:04

user2135970


People also ask

Can you pipe into grep?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

How do I use grep to find?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

What is pipe in bash?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.

What is the difference between a redirection and a pipe?

The difference between a pipe and a redirect is that while a pipe passes standard output as standard input to another command, a redirect sends a output to a file or reads a file as standard input to a command.


1 Answers

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

like image 90
sjwarner Avatar answered Sep 23 '22 19:09

sjwarner