Reputation: 11
I am trying to run a bash script to concatenate the contents of all text files in a folder/sub-folders and output that to a text file:
#!/bin/bash
find . -type f -name '*.txt' -exec cat {} + >> output.txt
This is creating an infinite loop.
My test directory is set up like:
~/desktop/Dir0/Dir1/Dir2
I created 2 text files:
~/desktop/Dir0/Dir1$ touch text1.txt && echo "Hello" >> text1.txt
~/desktop/Dir0/Dir1/Dir2$ touch text2.txt && echo "World" >> text2.txt
and then ran the .sh from within Dir0
The script did not conclude and running cat output.txt
listed a long file containing Hello\nWorld\nHello\nWorld\nHello\nWorld... infinitely.
Troubleshooting I have tried so far:
Anyone have an idea what might be happening here?
Upvotes: 1
Views: 141
Reputation: 6074
A more efficient way is to use xargs
:
find . -type f -name '*.txt' ! -name 'output.txt' -print0 | \
xargs -0 cat > output.txt
.txt
and not named output.txt
.xargs
in null termination mode (-0).cat
is called with more than 1 argument per execution.Upvotes: 1