How to Merge Text Files

Sometimes you want to merge a number of text files into one. I had a lot of logs file I wanted to merge into one. There are probably various ways to do.

  1. A programmer might say let me write a code to read from the files and append to a new file. That will require you looping through a folder to get a list of files, then creating a new file, reading from each of the files and append to the new file.
  2. A system Admin might think let me look for one command that will do this in few seconds.

Let’s assume I am a sys admin, I need a command to this. I remember cat actually means┬áconcatenate. Have you ever wondered why the command cat is called cat.

cat - concatenate files and print on the standard output

How to Merge Text Files and Log in a directory into One File

To merge all files in a directory

cat * > merged.log

To merge files with the same extension in the same directory e.g .txt

cat *.txt > merged.txt

If you tried this and found this useful, comment and share with your friends.

Leave a Reply