mac: concatenate contents of text files across many directories; append directory names to corresponding text file contents

1

In the following directory structure:

directory1
-subdirectoryA
---fileA.txt
-subdirectoryB
---fileB.txt
subdirectoryC
---fileC.txt

I would like to generate a text file containing the following text concatenated:

name of subdirectoryA
-text contained within fileA.txt
name of subdirectoryB
-text contained within fileB.txt
name of subdirectoryC
-text contained within fileC.txt

I was able to use the following commands to get the contents of the text files concatenated, but information I need the directory name to organize the output:

find ./prefix_common_to_all_target_directories* -name "*.txt" -exec cat '{}' \; > concatenated_extracted_info.txt

goober grape

Posted 2018-09-18T18:42:34.177

Reputation: 21

Answers

1

#!/bin/bash
while read mydir; do
    echo "${mydir}:" >> output.txt
    cat $mydir/*.txt >> output.txt
done < <(find test* -type d )

This is looping through all the directories within directory1 and does exactly what you want. Please note that you have to run this script within directory1.


Some explanations:

First find test* -type d runs which prints every subdirectory's name per line. This output is then fed into read mydir running everything within the while loop once for each line ($mydir is assigned to every line (aka subdirectoy name)).
Then the first line within the loop writes the directory name followed by a colon into output.txt, using >> which means "append to a file" (if file doesn't exist, it will be created).
The second line within the loop writes the contents of every *.txt file within the subdirectory to output.txt, again in "append-mode".


My test setup (with the above script saved as createfile.sh):

$ ls *
test1:
fileA.txt

test2:
fileB.txt

test3:
fileC.txt
$ bash createfile.sh
$ cat output.txt 
test1:
file content from dir1
test2:
test content from dir2
test3:
test content from dir3

confetti

Posted 2018-09-18T18:42:34.177

Reputation: 1 625

thank you! this worked perfectly! Now I will figure out why ;) – goober grape – 2018-09-19T00:43:36.410

@goobergrape I've added an explanation and simplified the code. If my answer helped you I'd appreciate you accepting (the on the left) it. :) – confetti – 2018-09-19T08:07:48.740

1Wow that's incredibly helpful and appreciated! Thank you for the explanation. I have checked the accept, thank you also for making me aware of this. Cheers! – goober grape – 2018-09-19T21:18:15.487