6

现在我经常这样做:

find * | grep py$ | xargs grep foo

我记得有一些实用程序可以用更少的输入来做到这一点,但是哪个?

更新:如果可能,我更喜欢使用 Bash shell。

4

8 回答 8

14

I love ack:

Which would you rather type?

$ grep pattern $(find . -type f | grep -v '\.svn')

$ ack pattern

于 2009-06-22T16:27:19.350 回答
4

你可能会发现你的 shell 对你有帮助。例如,在zsh中,您可以这样做

grep foo **/*.py

前提是 .py 文件的数量不超过命令允许的最大参数数量(64k?)。请注意,您可以限定文件通配符,例如

grep foo **/*.py(mh-5)

这将为您提供过去 5 小时内修改的所有内容。

于 2009-06-22T16:21:37.510 回答
3

Have you tried ack.

于 2009-06-22T16:29:00.583 回答
2

zsh 有递归通配符,所以你可以这样做

grep foo **/*.py

看马,没有找到:)

更新:哦,如果你做了很多事情,当然可以为它命名或编写一个函数

于 2009-06-22T16:25:00.677 回答
2

它被称为 grep *wink* :-)

当前目录下的所有py

grep -R foo *.py

当前和任何子目录中的所有文件

grep -R foo .
于 2009-06-22T16:25:37.493 回答
1
grep -r --include='*.py' foo *
于 2009-06-22T16:21:56.917 回答
1
find . -name "*.py" -exec grep -H foo '{}' ';'
于 2009-06-22T16:23:01.907 回答
0

I use something very much like your find/grep pair a lot, although with even more conditions -- excluding files in .svn directories, for example. I do this so much I just made scripts around these invocations, so I can call "src-grep ..." and have it do basically what you're doing here. (Then I added an optional extension for a number of context lines to pass to the grep -C flag, if supplied, and a separate version to grep the results for definition statements.)

This is more useful and faster than recursive grep for me.

于 2009-07-09T00:38:56.620 回答