Grep 不尊重 --exclude-dir

jww*_*jww 14 linux grep

/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
Run Code Online (Sandbox Code Playgroud)

-exclude-dir=/var/run和都试过了-exclude-dir="/var/run"。两者产生相同的结果。

为什么我的 grep 失败了?

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


CentOS 7.2,带 Grep:

$ grep --version
grep (GNU grep) 2.20
Copyright (C) 2014 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)

jeh*_*had 20

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

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

--exclude-dir=glob
    [..] skip any subdirectory whose base name matches glob.  [..]
Run Code Online (Sandbox Code Playgroud)

使固定

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

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

  • 不确定`/`s 做任何事情。grep 进入每个文件夹并在该点开始搜索。它实际上始终在搜索当前文件夹。如果你想排除一个子文件夹(例如`/home/me/dev/project1/js`)并且你正在从许多文件夹向上搜索(例如从`/home/me`开始),那么你需要指定您要排除的一个基本文件夹名称(例如`--exclude-dir=js`)。不幸的是,这也将排除层次结构中其他位置的任何同名子文件夹。如果你想要更多的控制,你可以尝试使用 `find` 和 `grep`。 (3认同)