现在我经常这样做:
find * | grep py$ | xargs grep foo
我记得有一些实用程序可以用更少的输入来做到这一点,但是哪个?
更新:如果可能,我更喜欢使用 Bash shell。
I love ack:
Which would you rather type?
$ grep pattern $(find . -type f | grep -v '\.svn')
$ ack pattern
你可能会发现你的 shell 对你有帮助。例如,在zsh中,您可以这样做
grep foo **/*.py
前提是 .py 文件的数量不超过命令允许的最大参数数量(64k?)。请注意,您可以限定文件通配符,例如
grep foo **/*.py(mh-5)
这将为您提供过去 5 小时内修改的所有内容。
Have you tried ack.
zsh 有递归通配符,所以你可以这样做
grep foo **/*.py
看马,没有找到:)
更新:哦,如果你做了很多事情,当然可以为它命名或编写一个函数
它被称为 grep *wink* :-)
当前目录下的所有py
grep -R foo *.py
当前和任何子目录中的所有文件
grep -R foo .
grep -r --include='*.py' foo *
find . -name "*.py" -exec grep -H foo '{}' ';'
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.