我正在grepping一大堆由git管理的代码,每当我做grep时,我都会看到成堆的形式的消息:

> grep pattern * -R -n
whatever/.git/svn: No such file or directory

有什么办法能让这些皱纹消失吗?


当前回答

使grep始终返回0状态的一个简单方法是使用|| true

 → echo "Hello" | grep "This won't be found" || true

 → echo $?
   0

如您所见,这里的输出值为0 (Success)

其他回答

如果你正在使用git存储库,我建议你使用git grep。你不需要传入-R或者路径。

git grep pattern

这将显示当前目录下的所有匹配项。

我得到了很多这些错误从Emacs运行“M-x rgrep”在Windows与/Git/usr/bin在我的路径。显然,在这种情况下,M-x rgrep使用“NUL”(Windows空设备)而不是“/dev/null”。我通过在.emacs中添加这个来修复这个问题:

;; Prevent issues with the Windows null device (NUL)
;; when using cygwin find with rgrep.
(defadvice grep-compute-defaults (around grep-compute-defaults-advice-null-device)
  "Use cygwin's /dev/null as the null-device."
  (let ((null-device "/dev/null"))
    ad-do-it))
(ad-activate 'grep-compute-defaults)

你试过xargs中的-0选项吗?就像这样:

ls -r1 | xargs -0 grep 'some text'

像这样的错误通常会被发送到“标准错误”流,你可以将其管道到一个文件中,或者在大多数命令中使其消失:

grep pattern * -R -n 2>/dev/null

我通常不让grep自己做递归。通常有一些您想要跳过的目录(。git, . svn…)

你可以像这样巧妙地使用别名:

find . \( -name .svn -o -name .git \) -prune -o -type f -exec grep -Hn pattern {} \;

乍一看,这似乎有些过分,但当您需要过滤掉一些模式时,它是相当方便的。