您如何处理“文件过多"?在Bash工作时出现问题? [英] How do you handle the "Too many files" problem when working in Bash?
问题描述
很多时候,我必须处理包含成千上万个文件的目录,进行文本匹配,替换等.如果我走标准路线,说
I many times have to work with directories containing hundreds of thousands of files, doing text matching, replacing and so on. If I go the standard route of, say
grep foo *
我收到太多文件错误消息,所以我最终还是这样做
I get the too many files error message, so I end up doing
for i in *; do grep foo $i; done
或
find ../path/ | xargs -I{} grep foo "{}"
但这并不是最佳选择(每个文件都创建一个新的grep进程).
But these are less than optimal (create a new grep process per each file).
这似乎更限制了程序可以接收的参数大小,因为for循环中的*可以正常工作.但是,无论如何,处理此问题的正确方法是什么?
This looks like more of a limitation in the size of the arguments programs can receive, because the * in the for loop works alright. But, in any case, what's the proper way to handle this?
PS:不要告诉我做grep -r,我知道这一点,我在考虑没有递归选项的工具.
PS: Don't tell me to do grep -r instead, I know about that, I'm thinking about tools that do not have a recursive option.
推荐答案
在较新版本的findutils中,find可以完成xargs的工作(包括glomming行为,因此仅使用所需的grep进程):
In newer versions of findutils, find can do the work of xargs (including the glomming behavior, such that only as many grep processes as needed are used):
find ../path -exec grep foo '{}' +
使用 +
而不是;
作为最后一个参数会触发此行为.
The use of +
rather than ;
as the last argument triggers this behavior.
这篇关于您如何处理“文件过多"?在Bash工作时出现问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!