Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate files in a subdirectory with Unix find execute and cat into a single file?

I can do this:

$ find .
.
./b
./b/foo
./c
./c/foo

And this:

$ find . -type f -exec cat {} \;
This is in b.
This is in c.

But not this:

$ find . -type f -exec cat > out.txt {} \;

Why not?

like image 814
JR Lawhorne Avatar asked Oct 03 '08 19:10

JR Lawhorne


1 Answers

find's -exec argument runs the command you specify once for each file it finds. Try:

$ find . -type f -exec cat {} \; > out.txt

or:

$ find . -type f | xargs cat > out.txt

xargs converts its standard input into command-line arguments for the command you specify. If you're worried about embedded spaces in filenames, try:

$ find . -type f -print0 | xargs -0 cat > out.txt
like image 112
Commodore Jaeger Avatar answered Nov 05 '22 23:11

Commodore Jaeger