How to append contents of multiple files into one file

LinuxBashUnix

Linux Problem Overview


I want to copy the contents of five files to one file as is. I tried doing it using cp for each file. But that overwrites the contents copied from the previous file. I also tried

paste -d "\n" 1.txt 0.txt

and it did not work.

I want my script to add the newline at the end of each text file.

eg. Files 1.txt, 2.txt, 3.txt. Put contents of 1,2,3 in 0.txt

How do I do it ?

Linux Solutions


Solution 1 - Linux

You need the cat (short for concatenate) command, with shell redirection (>) into your output file

cat 1.txt 2.txt 3.txt > 0.txt

Solution 2 - Linux

Another option, for those of you who still stumble upon this post like I did, is to use find -exec:

find . -type f -name '*.txt' -exec cat {} + >> output.file

In my case, I needed a more robust option that would look through multiple subdirectories so I chose to use find. Breaking it down:

find .

Look within the current working directory.

-type f

Only interested in files, not directories, etc.

-name '*.txt'

Whittle down the result set by name

-exec cat {} +

Execute the cat command for each result. "+" means only 1 instance of cat is spawned (thx @gniourf_gniourf)

 >> output.file

As explained in other answers, append the cat-ed contents to the end of an output file.

Solution 3 - Linux

if you have a certain output type then do something like this

cat /path/to/files/*.txt >> finalout.txt

Solution 4 - Linux

If all your files are in single directory you can simply do

cat * > 0.txt

Files 1.txt,2.txt, .. will go into 0.txt

Solution 5 - Linux

If all your files are named similarly you could simply do:

cat *.log >> output.log

Solution 6 - Linux

for i in {1..3}; do cat "$i.txt" >> 0.txt; done

I found this page because I needed to join 952 files together into one. I found this to work much better if you have many files. This will do a loop for however many numbers you need and cat each one using >> to append onto the end of 0.txt.

Edit:

as brought up in the comments:

cat {1..3}.txt >> 0.txt

or

cat {0..3}.txt >> all.txt

Solution 7 - Linux

Another option is sed:

sed r 1.txt 2.txt 3.txt > merge.txt 

Or...

sed h 1.txt 2.txt 3.txt > merge.txt 

Or...

sed -n p 1.txt 2.txt 3.txt > merge.txt # -n is mandatory here

Or without redirection ...

sed wmerge.txt 1.txt 2.txt 3.txt

Note that last line write also merge.txt (not wmerge.txt!). You can use w"merge.txt" to avoid confusion with the file name, and -n for silent output.

Of course, you can also shorten the file list with wildcards. For instance, in case of numbered files as in the above examples, you can specify the range with braces in this way:

sed -n w"merge.txt" {1..3}.txt

Solution 8 - Linux

if your files contain headers and you want remove them in the output file, you can use:

for f in `ls *.txt`; do sed '2,$!d' $f >> 0.out; done

Solution 9 - Linux

All of the (text-) files into one

find . | xargs cat > outfile

xargs makes the output-lines of find . the arguments of cat.

find has many options, like -name '*.txt' or -type.

you should check them out if you want to use it in your pipeline

Solution 10 - Linux

If the original file contains non-printable characters, they will be lost when using the cat command. Using 'cat -v', the non-printables will be converted to visible character strings, but the output file would still not contain the actual non-printables characters in the original file. With a small number of files, an alternative might be to open the first file in an editor (e.g. vim) that handles non-printing characters. Then maneuver to the bottom of the file and enter ":r second_file_name". That will pull in the second file, including non-printing characters. The same could be done for additional files. When all files have been read in, enter ":w". The end result is that the first file will now contain what it did originally, plus the content of the files that were read in.

Solution 11 - Linux

If you want to append contents of 3 files into one file, then the following command will be a good choice:

cat file1 file2 file3 | tee -a file4 > /dev/null

It will combine the contents of all files into file4, throwing console output to /dev/null.

Solution 12 - Linux

Send multi file to a file(textall.txt):

cat *.txt > textall.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteamView Question on Stackoverflow
Solution 1 - Linuxradical7View Answer on Stackoverflow
Solution 2 - Linuxmopo922View Answer on Stackoverflow
Solution 3 - LinuxEswar YagantiView Answer on Stackoverflow
Solution 4 - LinuxPoojaView Answer on Stackoverflow
Solution 5 - LinuxAeaRyView Answer on Stackoverflow
Solution 6 - LinuxfreddythunderView Answer on Stackoverflow
Solution 7 - LinuxFranView Answer on Stackoverflow
Solution 8 - LinuxbrunocrtView Answer on Stackoverflow
Solution 9 - LinuxHuubView Answer on Stackoverflow
Solution 10 - LinuxB.A.CooperView Answer on Stackoverflow
Solution 11 - LinuxAjit K'sagarView Answer on Stackoverflow
Solution 12 - LinuxNetwonsView Answer on Stackoverflow