我正在查找文件列表。

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

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


当前回答

以下是我的常用镜头:

grep YOURSTRING `find .`

它将打印文件名

其他回答

在bash中,以下代码是合适的:

find /dir -type f -print0 | xargs -0i cat {} | grep whatever

这将找到/dir目录下的所有文件,并安全地将文件名输送到xargs中,xargs将安全地驱动grep。

如果你在/dir目录下有成千上万的文件,跳过xargs不是一个好主意;Cat将由于参数列表长度过大而中断。Xargs会帮你处理的。

-print0参数用于查找与xargs的-0参数相匹配,以正确处理带有空格的文件名。xargs的-i参数允许您在cat命令行中插入所需的文件名。括号被从find导入cat命令的文件名所取代。

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.

列出并查看服务器上/ghi和/jkl目录下所有abc.def文件的内容

find /ghi /jkl -type f -name abc.def 2> /dev/null -exec ls {} \; -exec cat {} \;

要列出带有注释条目的abc.def文件并显示,请查看目录/ghi和/jkl中的这些条目

find /ghi /jkl -type f -name abc.def 2> /dev/null -exec grep -H ^# {} \;

这对我很有用

find _CACHE_* | while read line; do
    cat "$line" | grep "something"
done

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

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 {} \;