如何存储 find 生成的文件,即实际文件和内容作为文件而不是 find 的控制台输出?

How can I store the file that find produces i.e the actual file and contents as a file and not the console output of find?

目前我这样做是为了尝试存储 find 命令在我搜索名为 Myfile:

的文件时找到的实际文件及其内容
find /Users/Documents -name Myfile > outputfile

Myfile的内容是:

This is my original file.

然而,当我去执行一个操作时,例如 cat outputfile 它采用 find 命令的实际输出,而不是文件本身并显示:

/Users/Documents/Myfile

我期待着什么时候做 cat outputfile:

This is my original file.

如何存储文件本身并对其执行操作?我的特定用例是对文件执行静态分析,我需要将其作为参数传递。我最近发现使用 find。我不关心显示命令的输出。我想根据文件名查找文件,然后执行扫描并处理原始文件的内容,方法是将其存储为变量。我想将它存储为变量的原因是我可以将它作为参数传递给另一个程序。

要将文件路径存储在变量中,请使用:

fil=$(find /Users/Documents -name Myfile)

要实际重定向找到的文件的输出,请在 find 中使用 exec 标志,因此:

find /Users/Documents -name Myfile -exec cat '{}' \; > outputfile

如果您想将输出存储在变量中而不是重定向到文件中,您可以使用:

conts=$(find /Users/Documents -name Myfile -exec cat '{}' \;)

您可以使用的其他选项如下:

find /Users/Documents -name Myfile | xargs cat > outputfile


find /Users/Documents -name Myfile -print0 | xargs -0 cat > outputfile

-print0 允许在标准输出上打印完整文件路径,后跟空字符和 -0 xargs 标志有效地处理文件名中的 space。

此致