Alan Yu
Alan Yu

Reputation: 85

How to write a bash script to merge several file into one

I am using the Linux system. I would like to write a script to do these things below.

I have few folders named Folder 1, Folder 2, Folder 3 ...Folder 100 saved in test.In each folder I have a file named file.txt. How can I write a script to merge the files together. I type one script by myself,

for 
   file.txt in ~/test

do 
   cat $file.txt >> test.txt

done

However it shows that file.txt not a valid identifier due to it is in test/Folder 1.

Is that possible I can fix the code.

Many thanks

Upvotes: 3

Views: 134

Answers (1)

thonnor
thonnor

Reputation: 1246

Use the find command as shown below

find ~/test -name "file.txt" -exec cat {} \; >> test.txt

Doing this will search/find recursively in ~/test finding everything called file.txt and perform the cat command on everything it finds in the {} array finally outputting all of the files contents into file.txt

Upvotes: 2

Related Questions