我正在查找文件列表。

我如何将它输送到另一个实用程序,如cat,以便cat显示所有这些文件的内容?

然后,我将使用grep来搜索这些文件中的一些文本。


当前回答

Piping to another process (although this won't accomplish what you said you are trying to do): command1 | command2 This will send the output of command1 as the input of command2. -exec on a find (this will do what you want to do, but it's specific to find): find . -name '*.foo' -exec cat {} \; Everything between find and -exec are the find predicates you were already using. {} will substitute the particular file you found into the command (cat {} in this case); the \; is to end the -exec command. Send output of one process as command line arguments to another process: command2 `command1` For example: cat `find . -name '*.foo' -print` Note these are backquotes not regular quotes (they are under the tilde ~ on my keyboard). This will send the output of command1 into command2 as command line arguments. It's called command substitution. Note that file names containing spaces (newlines, etc) will be broken into separate arguments, though.

其他回答

为了实现这一点(使用bash),我将这样做:

cat $(find . -name '*.foo')

这就是所谓的“命令替换”,它在默认情况下去掉换行,这真的很方便!

更多信息请点击这里

您正在尝试在文件中查找文本吗?你可以简单地使用grep…

grep searchterm *

下面是我找到包含一些我感兴趣的内容的文件名的方法,只是一个简单的bash行,很好地处理文件名中的空格:

find . -name \*.xml | while read i; do grep '<?xml' "$i" >/dev/null; [ $? == 0 ] && echo $i; done

Piping to another process (although this won't accomplish what you said you are trying to do): command1 | command2 This will send the output of command1 as the input of command2. -exec on a find (this will do what you want to do, but it's specific to find): find . -name '*.foo' -exec cat {} \; Everything between find and -exec are the find predicates you were already using. {} will substitute the particular file you found into the command (cat {} in this case); the \; is to end the -exec command. Send output of one process as command line arguments to another process: command2 `command1` For example: cat `find . -name '*.foo' -print` Note these are backquotes not regular quotes (they are under the tilde ~ on my keyboard). This will send the output of command1 into command2 as command line arguments. It's called command substitution. Note that file names containing spaces (newlines, etc) will be broken into separate arguments, though.

这将只递归地打印文件的名称和内容。

find . -type f -printf '\n\n%p:\n' -exec cat {} \;

编辑(改进版): 这将只递归地打印文本(ascii)文件的名称和内容。

find . -type f -exec grep -Iq . {} \; -print | xargs awk 'FNR==1{print FILENAME ":" $0; }'

再试一次

find . -type f -exec grep -Iq . {} \; -printf "\n%p:" -exec cat {} \;