我在这里有以下命令来查看文件,看看它们是否包含某个正则表达式:
find / -type f | xargs grep -ilIs <regex>
它似乎做了它应该做的事情(查看桌面上的每个文件以获得表达式),但理想情况下我不会显示此错误消息,因为它只是对上面命令找到的文件中不匹配的单引号进行注释:
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
我尝试使用sed来消除错误消息,但是在命令之后使用| sed '/xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option/d'
并不会像我认为应该的那样删除它。我想知道你们中是否有人知道任何工具(当然,最好是最容易阅读和最少的打字),这些工具可以消除xargs错误消息。将-0
作为参数不返回任何内容,只返回以下内容:
xargs: argument line too long
发布于 2022-11-13 01:19:40
xargs
有自己的转义语法,例如:
$ echo 'file 1.txt' | xargs printf '<%s>\n'
<file>
<1.txt>
$ echo '"file 1.txt"' | xargs printf '<%s>\n'
<file 1.txt>
因此您不能为它提供原始文件,因为它们可以包含除NUL
字节以外的任何字符。
为了解决这个问题,xargs
的大多数实现都有允许处理NUL
-delimited记录的-0
开关,但是您需要在输入流中提供NUL
字节:
$ printf '%s\n' 'file 1.txt' 'file 2.txt' | xargs -0 printf '<%s>\n'
<file 1.txt
file 2.txt
>
$ printf '%s\0' 'file 1.txt' 'file 2.txt' | xargs -0 printf '<%s>\n'
<file 1.txt>
<file 2.txt>
最后,有三种方法可以正确地完成任务:
find ... -print0 | xargs -0 grep ...
find / -type f -print0 | xargs -0 grep -ilIs 'regex'
find ... -exec grep ... {} +
find / -type f -exec grep -ilIs 'regex' {} +
grep -R ...
grep -iRlIs 'regex' /
https://stackoverflow.com/questions/74416714
复制相似问题