理解 { } /dev/null \;将 find 与 -exec 一起使用时

Understanding the { } /dev/null \; when using find with -exec

我试图理解这一行:

find $HOME -name "*.c" -exec grep "find this string" {} /dev/null \;

我了解大部分内容,但我不确定出现在 {} 之后和 ; 之前的 /dev/null

find 找到每个 C 程序文件,然后对于每个文件,grep 查找包含字符串的行...然后将所有错误发送到 /dev/null

它用于强制 grep 打印匹配的文件名,仅在没有特定选项的 grep 中有用。看:

$ cat file
1
2
3

$ grep 2 file
2

$ grep 2 file /dev/null
file:2

过去需要它来获得该输出,但现在使用 GNU grep(和其他?)您可以改为这样做:

$ grep -H 2 file
file:2

您可能想查看课本上的保质期 ;-)。