bash grep 排除文件名模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24837635/
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
grep excluding file name pattern
提问by Chan Kim
I've read
Use grep --exclude/--include syntax to not grep through certain files
but in my CentOS6.4, when I do
我读过
使用 grep --exclude/--include 语法不通过某些文件
但在我的 CentOS6.4 中进行grep,当我这样做时
grep --exclude=*.cmd ckim * -r
I see lots of grepped lines from *.cmd files.
so it seems the exclude option is not working for me.
What is wrong?
of course I can do things like
我看到很多来自 *.cmd 文件的 grepped 行。
所以似乎排除选项对我不起作用。
怎么了?
我当然可以做类似的事情
grep ckim \`find . -name \*.c -print\`
but I want to know why the grep doesn't work.
但我想知道为什么 grep 不起作用。
回答by Tiago Lopo
You can quote the pattern:
你可以引用模式:
grep -r --exclude="*.cmd" "ckin" ./
PS. ./
is the current directory
附注。./
是当前目录
回答by Timofey Stolbov
Use .
as pathspec instead of *
.
使用.
作为pathspec代替*
。
grep -r --exclude \*.cmd ckim .
回答by Ajay
you could also do something like this
你也可以做这样的事情
grep -rn ckim * | grep -v '\.cmd'
回答by Sylvain Leroux
I see lots of grepped lines from *.cmd files. So it seems the exclude option is not working for me.
我看到很多来自 *.cmd 文件的 grepped 行。所以似乎排除选项对我不起作用。
There is a shell option called nullglob
that controls the expansion of shell patterns when there is no matching file.
nullglob
当没有匹配的文件时,有一个 shell 选项可以控制 shell 模式的扩展。
So, given the following environment:
因此,鉴于以下环境:
sh$ touch f.cmd g.sh
sh$ mkdir sub
sh$ echo ckim > sub/h.cmd
sh$ echo ckim > sub/i.cmd
On mysystem (where nullglob
is unset), the following command:
在我的系统上(nullglob
未设置的位置),以下命令:
grep --exclude=*.cmd ckim * -r
Is expanded ("understood") by the shell as:
被外壳扩展(“理解”)为:
grep --exclude=*.cmd ckim f.cmd g.sh sub -r
That is, I will recursively(-r
) search for the string skim
starting with f.cmd
, g.sh
and sub
but excluding any file matching the pattern '*.cmd'.
也就是说,我将递归(-r
)搜索字符串skim
开始f.cmd
,g.sh
和sub
,但不包括匹配模式“* .CMD”的任何文件。
The result is:
结果是:
# nullglob is unset
sh$ grep --exclude=*.cmd ckim * -r
sub/i.sh:ckim
BUTif in your environment the option nullglob
is set, the same command expands to:
但是,如果在您的环境nullglob
中设置了该选项,则相同的命令将扩展为:
grep ckim f.cmd g.sh sub -r
Notice how the whole--exclude=...
has disappeared. So the result is:
注意整体--exclude=...
是如何消失的。所以结果是:
# nullglob is set
sh$ grep --exclude=*.cmd ckim * -r
sub/i.sh:ckim
sub/h.cmd:ckim
An now, for the explanation. When the shell encounters a globpattern (i.e.: containing *
or ?
or a few more special characters), it expands it with the matching files. Butif there is no matching files, it either let the pattern as it (if nullglob
is notset) or replace it with "nothing" (if nullglob
is set).
现在,为解释。当 shell 遇到glob模式(即:包含*
或?
或更多特殊字符)时,它会用匹配的文件对其进行扩展。但是,如果没有匹配的文件,它要么让模式,因为它(如果nullglob
是未设置)或“无”替换(如果nullglob
已设置)。
Here the pattern is --include=*.cmd
(at wholesince there is no space in there). In the unlikely event you have a file matching this pattern it would have been replaced. Otherwise it is either let "as it" or completely removed -- depending on nullglob
.
这里的模式是--include=*.cmd
(整体上,因为那里没有空间)。万一您有一个与此模式匹配的文件,它将被替换。否则,要么让它“照原样”,要么完全删除——取决于nullglob
.
You can easely display, set (-s
) or unset (-u
) the status of nullglob
option of your current bash:
您可以轻松显示、设置 ( -s
) 或取消设置 ( -u
)nullglob
当前 bash的选项状态:
sh$ shopt nullglob
nullglob on
sh$ shopt -u nullglob
sh$ shopt nullglob
nullglob off
sh$ shopt -s nullglob
sh$ shopt nullglob
nullglob on
回答by John B
If you want to exclude certain files to grep from, you should use the -l
option.
如果您想从 grep 中排除某些文件,您应该使用该-l
选项。
grep -l --exclude=*.cmd ckim * -r