'Cat files into a new files in multiple sub directories

I would like to be able to find the sub directories in a folder Then located the .txt files in each sub folder Then CAT those files in each sub folder into a new .txt file within that folder.

This is what I am using now but it does not work:

find . -type d -exec find .{} -type f -iname .txt \; -exec cat {} >> all.txt \;

any help would be most appreciated.

Thanks B



Solution 1:[1]

Using a Bash for-Loop, this should work:

for sub_dir in $(find ./ -type d -not -name '.'); do
    cat $sub_dir/*.txt > $sub_dir/all.txt;
done

Use all the subdirs found by find (excluding the . directory) and issue a cat on each one of them.

Solution 2:[2]

find . -name "*.txt" -type f -exec cat {} + > all.txt

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tobi
Solution 2 notabot