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

时间:2022-11-27 13:51:28

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:

我正在通过git管理的一大堆代码,每当我做一个grep,我就会看到成堆成堆的信息:

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

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

有什么办法可以让这些线消失?

7 个解决方案

#1


212  

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

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

-s, --no-messages suppress error messages

-s, -无消息抑制错误消息

grep pattern * -s -R -n

#2


50  

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操作,我建议您使用git grep。你不需要输入-R或者路径。

git grep pattern

That will show all matches from your current directory down.

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

#3


6  

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

#4


4  

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

我通常不会让grep自己做递归。通常有一些目录需要跳过(。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.

乍一看,这似乎有点过头了,但当你需要过滤一些模式时,这是非常方便的。

#5


4  

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

你试过xargs的-0选项吗?是这样的:

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

#6


3  

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).

我曾多次看到过这种情况,当链接(指向不存在的文件的符号链接)被破坏时,grep试图搜索不存在的目标文件(因此出现了正确且准确的错误消息)。

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:

我通常不会在控制台执行sysadmin任务时麻烦,但在脚本中,我会查找带有“find”的文本文件,然后对每个文件进行grep:

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

Instead of:

而不是:

grep -nRHi -e "widehat" /etc

#7


0  

Use -I in grep.

使用grep - i。

Example: grep SEARCH_ME -Irs ~/logs.

例子:grep SEARCH_ME -Irs ~/log。

#1


212  

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

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

-s, --no-messages suppress error messages

-s, -无消息抑制错误消息

grep pattern * -s -R -n

#2


50  

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操作,我建议您使用git grep。你不需要输入-R或者路径。

git grep pattern

That will show all matches from your current directory down.

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

#3


6  

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

#4


4  

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

我通常不会让grep自己做递归。通常有一些目录需要跳过(。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.

乍一看,这似乎有点过头了,但当你需要过滤一些模式时,这是非常方便的。

#5


4  

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

你试过xargs的-0选项吗?是这样的:

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

#6


3  

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).

我曾多次看到过这种情况,当链接(指向不存在的文件的符号链接)被破坏时,grep试图搜索不存在的目标文件(因此出现了正确且准确的错误消息)。

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:

我通常不会在控制台执行sysadmin任务时麻烦,但在脚本中,我会查找带有“find”的文本文件,然后对每个文件进行grep:

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

Instead of:

而不是:

grep -nRHi -e "widehat" /etc

#7


0  

Use -I in grep.

使用grep - i。

Example: grep SEARCH_ME -Irs ~/logs.

例子:grep SEARCH_ME -Irs ~/log。