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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:38:09  来源:igfitidea点击:

bash function grep --exclude-dir not working

bashgrepfind

提问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-dirnot to be working -- it worked for me.)

(这个答案不适用于 OP,但可能对发现--exclude-dir不工作的其他人有帮助——它对我有用。)

回答by David W.

Do a man grepon 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 findto find the files you want, then use grepto 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 findprogram, the -oseparate out the various clauses. If a file has not been filtered out by a previous -pruneclause, it is passed to the next one. Once you've pruned out all of the .gitdirectories and all of the *.min.*files, you pass the results to the -execclause 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 -print0prints out all of the found files separated by the NULL character. The xargs -0will read in that list of files and pass them to the grepcommand. The -0tells xargsthat the file names are NULL separated and not whitespace separated. Some xargswill take --nullinstead of the -0parameter.

-print0出所有找到的文件打印相隔NULL字符。该xargs -0会在文件的该列表读取,并将其传递给grep命令。该-0通知xargs,文件名是NULL分离,而不是空格隔开。有些xargs--null代替-0参数。