How to grep files in git repo?

You might consider a non-git solution in this case.

find itself has the ability to do what you want in a more efficient manner than piping its results into grep:

find . -name 'middleware*'

You will need to quote the pattern so that the * isn't expanded by the shell before being passed to find.

There is a powerful program called ack that is, well, better than grep, and one of my favorite uses for ack is exactly what you've mentioned -- finding files that match a pattern within a tree. ack uses perl regexps, not shell fileglobs, though.

ack -g middleware

If you want to search within those files, ack lets you do that more easily than writing a shell loop over the results of find that greps within each file. Compare the two and see which one you prefer:

for f in $(find . -name 'middleware*')
do
    grep 'pattern in file' $f
done

versus

ack -G 'middleware' 'pattern in file'

I highly recommend ack as something to add to your toolkit.


I think git ls-files will do the trick for you.

So:

 git ls-files "*middleware*"

Maybe you want git ls-files which lists the files in the index? (and automatically adjusts for your current directory inside the git work directory)


git now has matured search functionality (as the previous poster mentioned). You can search file names, extensions, by programming language.. You can search inside at file contents... etc.

You search when you log into GitHub, at the search field in the upper - left of the screen.

See this for details: https://help.github.com/en/articles/searching-code

Tags:

Git

Grep