Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git grep by file extensions

I know that, if I wanted to grep for a pattern only on files with certain extensions, I could do this:

// searches recursively and matches case insensitively in only javascript files // for "res" from the current directory grep -iIr --include=*.js res ./ 

I've been trying to search for a way to do this via git grep as well (to take advantage of git grep's speed from the indexes it stores with its tree), but to no avail. I saw here that excluding certain file types is not possible.

like image 665
Vinay Avatar asked Dec 13 '12 20:12

Vinay


People also ask

How do I grep in a specific extension?

Specifying File Type To do so, we can use a special type of option that starts with two hyphens: $ grep -ri "hello" --include=*. cc Test/test.cc:Hello World!

What files are searchable by git grep?

`git grep` command is used to search in the checkout branch and local files.

How do I grep specific files recursively?

To take the explanation from HoldOffHunger's answer below: grep : command. -r : recursively.

How do I grep selected files?

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'.


2 Answers

Yes, for example:

git grep res -- '*.js' 
like image 152
CB Bailey Avatar answered Oct 03 '22 08:10

CB Bailey


Try doing this :

find . -type f -iname '*.js' -exec grep -i 'pattern' {} + 
like image 27
Gilles Quenot Avatar answered Oct 03 '22 10:10

Gilles Quenot