Inside my main folder, I have multiple sub-folders and each sub folder contains multiple files. I want to merge these files in every sub-folder.
So I am trying to do something like this:
cd ../master-folder
for file in $( find . -name "*.txt" );
do
cat "all the text files in this sub folder" > "name of the subfolder.txt"
rm "all the previous text files excluding the merged output obviously"
done
Appreciate the help! Thank you.
I would do it like this, if the order of the files doesn't matter :
The first find looks for subdirectories.
The second one appends all subfile's content to a file
The third one deletes the subfiles.
This doesn't work if there are recursive subdirectories. If you want this, remove '-maxdepth 1'
Why not visit each directory recursively? Something along the lines of:
Caveat: If you have sym links that create cycles in your directory tree then this is a bad idea.