Search code examples
catsubdirectory

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

  • 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.