Skip to content
Advertisement

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement