Error in concatenating multiple files from subdirectories

I want to concatenate multiple files recursively from sub-directories intoone file in Linux.

I saved the following script as script.sh in $HOME/testing1 where I have several subdirectories and .txt files into them. I ran script.sh from the command prompt of $HOME/testing1 as ./script.sh. But it deleted all the files from my sub-directories. Can anybody tell me what was wrong and write the correct script for me?

The only messages I got:

Code:
cat: file11.txt: input file is output filecat: file22.txt: input file is output file............
Here is the script I have used:

ogdir=$HOME/testing1 ## adjust to tastefor dir in "$logdir"/*/do(cd "$dir"files=( *.txt )cat "${files[@]}" > "${PWD##*/}.txt"rm "${files[@]}")done

---------- Post updated at 01:31 PM ---------- Previous update was at 01:29 PM ----------

I want to concatenate multiple files recursively from sub-directories into one file in Linux.

I saved the following script as script.sh in $HOME/testing1 where I have several subdirectories and .txt files into them. I ran script.sh from the command prompt of $HOME/testing1 as ./script.sh. But it deleted all the files from my sub-directories. Can anybody tell me what was wrong and write the correct script for me?

The only messages I got:

cat: file11.txt: input file is output file
cat: file22.txt: input file is output file
 

Here is the script I have used:

l

ogdir=$HOME/testing1 ## adjust to taste
for dir in "$logdir"/*/
do
(
cd "$dir"
files=( *.txt )
cat "${files[@]}" > "${PWD##*/}.txt"
rm "${files[@]}"
)
done

The "ogdir" and "logdir" is just a misplaced code tag.

We definitely need to see a reasonable sample of the directory tree and contents. Also, what you expect "concatonate" to do in the context of your files in your directory tree?
This is not so much about correcting some script you found in a post on the Internet, but about finding a solution to your problem in your environment.

The script posted has many design flaws were it to be used as a general tool. It was posted by cfajohnson in response to a specific post a month ago.
I assume you have bash because this line is a syntax error in my unix Posix shells:

One of the more obvious issues is that it canot be run twice or you could end up with all your ".txt" files deleted (including the concatonated one). There is no way to unconcatonate after this script has been run once.

If I have to guess I would say that [s]he has a directory name file11 and file22, as children of testing1, which have themselves a file11.txt and file22.txt
When it hits "${PWD##*/}.txt" the directory names are used to send output to a file name as such, conflicting with the file already open.

But this is only a guess.