我这里有这些命令用于查看文件以查看它们是否包含特定的正则表达式:
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
xargs有它自己的转义语法,例如:
$ echo 'file 1.txt' | xargs printf '<%s>\n'
<file>
<1.txt>
$ echo '"file 1.txt"' | xargs printf '<%s>\n'
<file 1.txt>
Run Code Online (Sandbox Code Playgroud)
因此,您不能向其提供原始文件路径,因为它们可以包含除NUL字节之外的任何字符。
为了解决这个问题,大多数实现都有允许处理 - 分隔记录的开关xargs,但您需要在输入流中提供字节:-0NULNUL
$ 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>
Run Code Online (Sandbox Code Playgroud)
最后,您可以通过三种方法来正确完成任务:
find ... -print0 | xargs -0 grep ...find / -type f -print0 | xargs -0 grep -ilIs 'regex'
Run Code Online (Sandbox Code Playgroud)
find ... -exec grep ... {} +find / -type f -exec grep -ilIs 'regex' {} +
Run Code Online (Sandbox Code Playgroud)
grep -R ...grep -iRlIs 'regex' /
Run Code Online (Sandbox Code Playgroud)