I like the unix find
command but I always find it too 'fiddly' to use when I want to search through my project looking for a piece of text in any file in any directory or subdirectory.
Is there an easier way to do this?
How can I search my directory tree for contents within a file for a git managed project?
19.6k views Asked by Michael Durrant AtThere are 5 answers
git grep
is one way to do this, but it'll ignore untracked files (so it's not exactly equivalent to whatever you're doing with find
). A few other ways to get at this that avoid find
's curious syntax:
grep -r "<string>" /path/to/repo
You might also try my personal favorite grep
alternative, ack, which outperforms both grep
and git grep
in my anecdotal experience:
ack "<string>" /path/to/repo ;# path is unnecessary if you're already in the repo
If you are worried about slow searches in big projects, you should take a look at The Silver Searcher. It is extremely fast. A test run for the string "TODO" in a 75 000 line project took under 10 ms.
Excerpt from README:
What's so great about Ag?
- It is an order of magnitude faster than ack.
- It ignores file patterns from your .gitignore and .hgignore.
- If there are files in your source repo you don't want to search, just add their patterns to a .ignore file. (cough *.min.js cough)
- The command name is 33% shorter than ack, and all keys are on the home row!
Ag is quite stable now. Most changes are new features, minor bug fixes, or performance improvements. It's much faster than Ack in my benchmarks:
ack test_blah ~/code/ 104.66s user 4.82s system 99% cpu 1:50.03 total ag test_blah ~/code/ 4.67s user 4.58s system 286% cpu 3.227 total
Ack and Ag found the same results, but Ag was 34x faster (3.2 seconds vs 110 seconds). My ~/code directory is about 8GB. Thanks to git/hg/ignore, Ag only searched 700MB of that.
It can be installed through most package repositories, for example:
apt-get install silversearcher-ag
Try grep
utility:
- For searching a string in the directory tree recursively :
use: grep -rl alvin .
- Search multiple subdirectories:
Your recursive grep
searches don't have to be limited to just the current directory. This next example shows how to recursively search two unrelated directories for the case-insensitive string "alvin":
grep -ril alvin /home/cato /htdocs/zenf
- Using egrep recursively:
You can also perform recursive searches with the egrep
command, which lets you search for multiple patterns at one time.
egrep -ril 'aja|alvin' .
Note that in this case, quotes are required around the search pattern.
Summary: grep -r notes:
A few notes about the grep -r
command:
This grep command doesn't make much sense unless you use it with the -l (lowercase "L") flag as well. This flag tells grep to print the matching filenames.
Don't forget to list one or more directories at the end of your grep command. If you forget to add any directories, grep will attempt to read from standard input (as usual).
As shown, you can use other normal grep flags as well, including -i to ignore case, -v to reverse the meaning of the search, etc.
git grep
"your text string", from the applcation's base directory is a great way to do this.Also as Christopher points out
ack
is useful.His install method didn't work for me. I had to do:
and then for convenience
which I'll also add to my
~/.bash_aliases
file.