bash 函数 grep --exclude-dir 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19962645/
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
bash function grep --exclude-dir not working
提问by Noah Duncan
I have the following function defined in my .bashrc, but for some reason the --exclude-dir option is not excluding the .git directory. Can anyone see what I've done wrong? I'm using Ubuntu 13.10 if that helps.
我在 .bashrc 中定义了以下函数,但出于某种原因,--exclude-dir 选项不排除 .git 目录。谁能看到我做错了什么?如果有帮助,我正在使用 Ubuntu 13.10。
function fif # find in files
{
pattern=${1?" Usage: fif <word_pattern> [files pattern]"};
files=${2:+"-iname \"\""};
grep "$pattern" --color -n -H -s $(find . $files -type f) --exclude-dir=.git --exclude="*.min.*"
return 0;
}
回答by dinosaur
Make sure not to include a trailing slash when you specify the directory to exclude. For example:
指定要排除的目录时,请确保不要包含尾部斜杠。例如:
Do this:
做这个:
$ grep -r --exclude-dir=node_modules firebase .
NOT this:
不是这个:
$ grep -r --exclude-dir=node_modules/ firebase .
(This answer not applicable to OP, but may be helpful for others who find --exclude-dir
not to be working -- it worked for me.)
(这个答案不适用于 OP,但可能对发现--exclude-dir
不工作的其他人有帮助——它对我有用。)
回答by David W.
Do a man grep
on your system, and see what version you have. Your version of grep may not be able to use --exclude-dirs
.
man grep
在你的系统上做一个,看看你有什么版本。您的 grep 版本可能无法使用--exclude-dirs
.
You're really better off using find
to find the files you want, then use grep
to parse them:
您最好使用find
查找所需的文件,然后使用grep
解析它们:
$ find . -name '.git' -type d -prune \
-o -name "*.min.*" -prune \
-o -type f -exec grep --color -n -H {} "$pattern" \;
I'm not a fan of the recursive grep
. Its syntax has become bloated, and it's really unnecessary. We have a perfectly good tool for finding files that match a particular criteria, thank you.
我不喜欢递归grep
。它的语法变得臃肿了,真的没有必要。我们有一个非常好的工具来查找符合特定条件的文件,谢谢。
In the find
program, the -o
separate out the various clauses. If a file has not been filtered out by a previous -prune
clause, it is passed to the next one. Once you've pruned out all of the .git
directories and all of the *.min.*
files, you pass the results to the -exec
clause that executes your grep command on that one file.
在find
程序中,-o
将各个子句分开。如果一个文件没有被前一个-prune
子句过滤掉,它就会被传递到下一个子句。删除所有.git
目录和所有*.min.*
文件后,将结果传递给对该文件-exec
执行 grep 命令的子句。
Some people prefer it this way:
有些人更喜欢这样:
$ find . -name '.git' -type d -prune \
-o -name "*.min.*" -prune \
-o -type f -print0 | xargs -0 grep --color -n -H "$pattern"
The -print0
prints out all of the found files separated by the NULL character. The xargs -0
will read in that list of files and pass them to the grep
command. The -0
tells xargs
that the file names are NULL separated and not whitespace separated. Some xargs
will take --null
instead of the -0
parameter.
该-print0
出所有找到的文件打印相隔NULL字符。该xargs -0
会在文件的该列表读取,并将其传递给grep
命令。该-0
通知xargs
,文件名是NULL分离,而不是空格隔开。有些xargs
会--null
代替-0
参数。