Shell Script to append files

I have several content (text) files in a folder called "content"

I have several google ads (text) files in a folder called "google_ads"

Example:

/content
    /java
       websphere.txt
       android.txt
   /microsoft
      framework.txt
      /c_sharp
         linq.txt
/google_ads
    /java
       websphere.txt
       android.txt
   /microsoft
      framework.txt
      /c_sharp
         linq.txt

I want to create a shell script that appends the files together and produces them in a "publish" folder

/publish
     /java
        websphere.txt
        android.txt
    /microsoft
       framework.txt
       /c_sharp
          linq.txt

How do I go about doing this?

cat is your friend there

cat file_one file_two file_three > publish_folder/big_mother_file
1 Like

yes. but I have 100s / 1000s of files (in several sub directories).

How do I do this in a recursive fashion?

yes

cat content/*.txt google_ads/*.txt foobar/*.txt >publish.file

if you hit a maximum arguments error try this.

find . -name \*.txt -exec cat {} \; > publish.file

Try to run this on a script. You should be on the parent directory of content and google_ads

#!/bin/bash

function main {
	while read LINE; do
		LINE=${LINE#*/}
		echo "creating directory publish/$LINE..." || {
			echo "failed."
			return 1
		}
		mkdir -p "publish/$LINE"
	done < <(exec find content/ -type d)

	while read LINE; do
		LINE=${LINE#*/}
		echo "making file publish/$LINE..."
		{
			cat "content/$LINE"
			[[ ! -f google_ads/$LINE ]] || cat "google_ads/$LINE"
		} > "publish/$LINE"
		[[ $? -eq 0 ]] || {
			echo "failed."
			return 1
		}
	done < <(exec find content/ -type f -iname '*.txt')
}

main