我怎样才能让 grep 不打印出“没有这样的文件或目录”?错误?

发布于 2024-11-16 20:10:15 字数 182 浏览 1 评论 0原文

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

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

有什么方法可以让这些行消失吗?

I'm grepping through a large pile of code managed by git, and whenever I do a grep, I see piles and piles of messages of the form:

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

Is there any way I can make those lines go away?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

樱花落人离去 2024-11-23 20:10:15

您可以使用 -s--no-messages 标志来抑制错误。

-s, --no-messages 抑制错误消息

grep pattern * -s -R -n

You can use the -s or --no-messages flag to suppress errors.

-s, --no-messages suppress error messages

grep pattern * -s -R -n
久隐师 2024-11-23 20:10:15

如果您要通过 git 存储库进行 grep,我建议您使用 git grep。您不需要传入 -R 或路径。

git grep pattern

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

If you are grepping through a git repository, I'd recommend you use git grep. You don't need to pass in -R or the path.

git grep pattern

That will show all matches from your current directory down.

暖伴 2024-11-23 20:10:15

类似的错误通常会发送到“标准错误”流,您可以通过管道将其传输到文件或在大多数命令中使其消失:

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

Errors like that are usually sent to the "standard error" stream, which you can pipe to a file or just make disappear on most commands:

grep pattern * -R -n 2>/dev/null
鹿港巷口少年归 2024-11-23 20:10:15

我已经看到这种情况发生了好几次,当链接断开(指向不存在的文件的符号链接)时,grep 尝试搜索不存在的目标文件(因此出现正确且准确的错误消息)。

我通常不会在通过控制台执行系统管理任务时打扰,但从脚本中我确实使用“find”查找文本文件,然后 grep 每个文件:

find /etc -type f -exec grep -nHi -e "widehat" {} \;

而不是:

grep -nRHi -e "widehat" /etc

I have seen that happening several times, with broken links (symlinks that point to files that do not exist), grep tries to search on the target file, which does not exist (hence the correct and accurate error message).

I normally don't bother while doing sysadmin tasks over the console, but from within scripts I do look for text files with "find", and then grep each one:

find /etc -type f -exec grep -nHi -e "widehat" {} \;

Instead of:

grep -nRHi -e "widehat" /etc
胡大本事 2024-11-23 20:10:15

我通常不让 grep 自己进行递归。通常有一些目录你想跳过(.git,.svn ...)

你可以用这样的立场做聪明的别名:

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

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

I usually don't let grep do the recursion itself. There are usually a few directories you want to skip (.git, .svn...)

You can do clever aliases with stances like that one:

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

It may seem overkill at first glance, but when you need to filter out some patterns it is quite handy.

紫瑟鸿黎 2024-11-23 20:10:15

您是否尝试过 xargs 中的 -0 选项?像这样的东西:

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

Have you tried the -0 option in xargs? Something like this:

ls -r1 | xargs -0 grep 'some text'
好多鱼好多余 2024-11-23 20:10:15

在 grep 中使用 -I

示例:grep SEARCH_ME -Irs ~/logs

Use -I in grep.

Example: grep SEARCH_ME -Irs ~/logs.

北渚 2024-11-23 20:10:15

我将 stderr 重定向到 stdout,然后使用 grep 的反向匹配 (-v) 来排除我想要隐藏的警告/错误字符串:

grep -r <pattern> * 2>&1 | grep -v "No such file or directory"

I redirect stderr to stdout and then use grep's invert-match (-v) to exclude the warning/error string that I want to hide:

grep -r <pattern> * 2>&1 | grep -v "No such file or directory"
痴情换悲伤 2024-11-23 20:10:15

我在 Windows 上的 Emacs 中运行“Mx rgrep”时遇到了很多这样的错误,并且路径中有 /Git/usr/bin 。显然在这种情况下, Mx rgrep 使用“NUL”(Windows 空设备)而不是“/dev” /空”。我通过将其添加到 .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)

I was getting lots of these errors running "M-x rgrep" from Emacs on Windows with /Git/usr/bin in my PATH. Apparently in that case, M-x rgrep uses "NUL" (the Windows null device) rather than "/dev/null". I fixed the issue by adding this to .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)
残龙傲雪 2024-11-23 20:10:15

让 grep 始终返回零状态的一种简单方法是使用 || true

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

 → echo $?
   0

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

One easy way to make grep return zero status all the time is to use || true

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

 → echo $?
   0

As you can see the output value here is 0 (Success)

戏蝶舞 2024-11-23 20:10:15

很多答案,但没有一个有效,叹息

 grep "nick-banner" *.html -R -n

它不起作用:

grep: *.html: No such file or directory

Many answers, but none works, sigh

 grep "nick-banner" *.html -R -n

It does not work:

grep: *.html: No such file or directory
疯了 2024-11-23 20:10:15

问题:

这让我抓狂。我在(Google)Sun下尝试了所有方法,但这个 grep 没有任何效果,它只是重复出现有关“sysctl:读取密钥...的错误" 在最终打印匹配之前:

sudo sysctl -a | grep vm.min_free_kbytes

解决方案:

没有任何效果,直到我顿悟:如果我在前面而不是后面进行过滤会怎样?... 是的:有效:

sysctl -a --ignore 2>/dev/null | -a --ignore 2>/dev/null | grep vm.min_free_kbytes

结论:

显然不是每个命令都会有 --ignore 开关,但这是我如何解决BEFORE我的过滤问题的一个例子grep。别太狭隘了,你追着尾巴去追求一些行不通的东西;-)

Problem:

This drove me bananas. I tried everything under the (Google) Sun and nothing worked with this grep which just puked repeated errors about "sysctl: reading key ..." before finally printing the match:

sudo sysctl -a | grep vm.min_free_kbytes

Solution:

Nothing worked UNTIL I had an epiphany: What if I filtered in the front rather than at the back?... Yup: that worked:

sysctl -a --ignore 2>/dev/null | grep vm.min_free_kbytes

Conclusion:

Obviously not every command will have the --ignore switch, but it's an example of how I got around the problem filtering BEFORE my grep. Don't get so blinkered you chase your tail pursuing something that won't work ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文