Dears,
I have tried a combination of "find" and "cat" in a bash script to duplicate contents of hundreds of files in sub-directories:
find ./inlet3/* -type f -name 'U' -exec bash -c 'cat "{}"' \; > temp -exec bash -c 'cat temp >> "{}"' \;
Looking at the given command, I intend to first copy the content of each "U" into a temporary file named "temp", which is supposed to be created each time in the directory where the bash script exists. This part should be achieved by the following method:
-exec bash -c 'cat "{}"' \; > temp
The content of "temp" should be subsequently appended to "U", to have a file with duplicated content. This part is supposed to be done by the given method:
-exec bash -c 'cat temp >> "{}"' \;
However, the command does not give me the desired result. I would anticipate that for each "U" found via find, "temp" would be overwritten. But I see that the content of previously found "U"s are appended to "temp", accumulating its content. If I try to remove "temp" with the following method:
-exec rm -rf temp \;
then I will receive an error:
cat: temp: No such file or directory
There are some other -exec options that I have omitted for simplicity.
Could anybody say where I might be wrong? I appreciate any hint.
Kind regards.