Grep不兑现--exclude-dir


12

/var由于挂在上,我无法搜索/var/run。我尝试排除/var/run,但未产生预期结果:

$ sudo grep -IR --exclude-dir="/var/run" '45.78.157.165' /var | egrep -v '(audit|access)'
/var/log/secure:Jun 21 14:08:34 cryptopp sshd[19729]: error: Received disconnect from 199.91.135.157: 3: com.jcraft.jsch.JSchException: reject HostKey: 45.78.157.165 [preauth]
/var/log/secure-20160626:Jun 21 14:08:34 cryptopp sshd[19729]: error: Received disconnect from 199.91.135.157: 3: com.jcraft.jsch.JSchException: reject HostKey: 45.78.157.165 [preauth]
/var/log/secure-20160626:Jun 21 14:08:34 cryptopp sshd[19729]: error: Received disconnect from 199.91.135.157: 3: com.jcraft.jsch.JSchException: reject HostKey: 45.78.157.165 [preauth]
grep: /var/run/saslauthd/mux: No such device or address
grep: /var/run/dbus/system_bus_socket: No such device or address
grep: /var/run/rpcbind.sock: No such device or address
grep: /var/run/udev/control: No such device or address

我已经尝试过-exclude-dir=/var/run-exclude-dir="/var/run"。两者产生相同的结果。

为什么我的grep失败了?

如何/var/run从递归grep中排除?


使用Grep的CentOS 7.2:

$ grep --version
grep (GNU grep) 2.20
Copyright (C) 2014 Free Software Foundation, Inc.

1
您是否尝试了--exclude-dir=/var/run不带引号的情况?
JakeGould '16

@JakeGould-是的;无论有没有。让我补充一下这个问题。
jww '16

@JakeGould没什么区别:grep在任何情况下,该过程都看不到引号。Bash会扩展它们,并且无需执行替换操作,因此可以将其删除。我知道的所有外壳都是这种情况。
wchargin '16

Answers:


18

我认为这可能是因为您明确要求grep从中进行递归搜索/var,并且/var/run与下的SUBDIRECTORY不匹配/var

请参见grep手册页,其中指出:

--exclude-dir=glob
    [..] skip any subdirectory whose base name matches glob.  [..]

固定

因此,要修复您的命令,请更改排除模式,即:

sudo grep -IR --exclude-dir="run" '45.78.157.165' /var | egrep -v '(audit|access)'

我对此有类似的问题。您将如何更改grep -r --exclude-dir=./root/js sometext .以从当前位置排除子文件夹?
psynnott

3
不确定/s做任何事情。grep进入每个文件夹,然后开始搜索。它实际上总是在搜索当前文件夹。如果您要排除一个子文件夹(例如/home/me/dev/project1/js),并且要从多个文件夹向上搜索(例如,从开始/home/me),则需要指定一个要排除的基本文件夹名称(例如--exclude-dir=js)。不幸的是,这还将排除层次结构中其他位置的任何同名子文件夹。如果您希望获得更多控制权,可以尝试使用findwith grep
jehad '16
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.