如何在Unix子目录中连接文件,将execute和cat连接到一个文件中?

JR *_*rne 9 unix command-line

我可以做这个:

$ find .
.
./b
./b/foo
./c
./c/foo
Run Code Online (Sandbox Code Playgroud)

还有这个:

$ find . -type f -exec cat {} \;
This is in b.
This is in c.
Run Code Online (Sandbox Code Playgroud)

但不是这个:

$ find . -type f -exec cat > out.txt {} \;
Run Code Online (Sandbox Code Playgroud)

为什么不?

Com*_*ger 28

find的-exec参数为它找到的每个文件运行您指定的命令一次.尝试:

$ find . -type f -exec cat {} \; > out.txt
Run Code Online (Sandbox Code Playgroud)

要么:

$ find . -type f | xargs cat > out.txt
Run Code Online (Sandbox Code Playgroud)

xargs将其标准输入转换为您指定的命令的命令行参数.如果您担心文件名中的嵌入空格,请尝试:

$ find . -type f -print0 | xargs -0 cat > out.txt
Run Code Online (Sandbox Code Playgroud)


小智 5

嗯...当你输出out.txt到当前目录时,find似乎在递归

尝试类似的东西

find . -type f -exec cat {} \; > ../out.txt
Run Code Online (Sandbox Code Playgroud)