如何在我的目录树中搜索 git 管理项目的文件中的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11887594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I search my directory tree for contents within a file for a git managed project?
提问by Michael Durrant
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?
我喜欢 unixfind
命令,但当我想在我的项目中搜索任何目录或子目录的任何文件中的一段文本时,我总是发现它太“繁琐”了。
有没有更简单的方法来做到这一点?
采纳答案by Michael Durrant
git grep
"your text string", from the applcation's base directory is a great way to do this.
git grep
“您的文本字符串”,来自应用程序的基本目录是一个很好的方式来做到这一点。
Also as Christopher points out ack
is useful.
也正如克里斯托弗指出的那样ack
很有用。
His install method didn't work for me. I had to do:
他的安装方法对我不起作用。我必须做:
sudo apt-get install ack-grep
and then for convenience
然后为了方便
alias ack='ack-grep ' # So that I can just type ack "string"
which I'll also add to my ~/.bash_aliases
file.
我也会将其添加到我的~/.bash_aliases
文件中。
回答by Prabhat Kumar Singh
Try grep
utility:
尝试grep
实用程序:
- For searching a string in the directory tree recursively:
- 在目录树中递归搜索字符串:
use: grep -rl alvin .
用: 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
搜索不必仅限于当前目录。下一个示例显示如何递归搜索两个不相关的目录以查找不区分大小写的字符串“alvin”:
grep -ril alvin /home/cato /htdocs/zenf
- Using egrep recursively:
- 递归使用 egrep:
You can also perform recursive searches with the egrep
command, which lets you search for multiple patterns at one time.
您还可以使用该egrep
命令执行递归搜索,该命令可让您一次搜索多个模式。
egrep -ril 'aja|alvin' .
Note that in this case, quotes are required around the search pattern.
请注意,在这种情况下,搜索模式周围需要引号。
Summary: grep -r notes:
总结:grep -r 注释:
A few notes about the grep -r
command:
关于grep -r
命令的一些注意事项:
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.
除非您将它与-l(小写“L”)标志一起使用,否则此 grep 命令没有多大意义。此标志告诉 grep 打印匹配的文件名。
不要忘记在 grep 命令的末尾列出一个或多个目录。如果您忘记添加任何目录,grep 将尝试从标准输入中读取(像往常一样)。
如图所示,您也可以使用其他普通的 grep 标志,包括 -i 忽略大小写,-v 反转搜索的含义等。
回答by Christopher
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:
git grep
是执行此操作的一种方法,但它会忽略未跟踪的文件(因此它并不完全等同于您正在使用的任何文件find
)。一些其他的方法来解决这个问题,以避免find
奇怪的语法:
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:
您也可以尝试我个人最喜欢的grep
选择,ACK,其性能优于grep
和git grep
我根据经验:
ack "<string>" /path/to/repo ;# path is unnecessary if you're already in the repo
回答by Hubro
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.
如果您担心大型项目中的搜索速度缓慢,您应该看看The Silver Searcher。它非常快。在 75 000 行项目中对字符串“TODO”的测试运行时间不到 10 毫秒。
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.
Ag有什么了不起?
- 它比ack快一个数量级。
- 它会忽略 .gitignore 和 .hgignore 中的文件模式。
- 如果您不想搜索源存储库中的文件,只需将它们的模式添加到 .ignore 文件中。(咳*.min.js咳)
- 命令名比ack短33%,所有key都在home行!
Ag现在相当稳定。大多数更改是新功能、小错误修复或性能改进。在我的基准测试中,它比 Ack 快得多:
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 和 Ag 发现了相同的结果,但 Ag 快了 34 倍(3.2 秒对 110 秒)。我的 ~/code 目录大约是 8GB。感谢 git/hg/ignore,Ag 只搜索了其中的 700MB。
It can be installed through most package repositories, for example:
它可以通过大多数软件包存储库安装,例如:
apt-get install silversearcher-ag
回答by Bohdan
Grep is simplest approach.
Grep 是最简单的方法。
grep -r 'text to find' .