How to Combine Two Files in Linux

How to Combine Two Files in Linux

Often Linux developers and system administrators need to combine multiple files into a single file. For example, you may need to combine SSL certificate files into a single file bundle. There are several third party tools available for this purpose. But you can easily do this using cat command which is available by default in all Linux distributions. In this article, we will learn how to combine two files in Linux.

How to Combine Two Files in Linux

Let us say you want to combine files /home/file1.txt and /home/file2.txt. There are several ways to do this. We will look at each case one by one.

1. Show Combined Data of Two Files

If you want to simply display the contents of two or more files without actually changing either of the files, or creating a new file, just run the cat command followed by paths of files whose content you want to be combined, one after the other.

$ cat /home/file1.txt /home/file2.txt

Similarly, you can also combine more than two files if you want, as shown below.

$ cat /home/file1.txt /home/data/file2.txt /etc/data/file3.txt

Please provide full file paths instead of providing relative file paths. Otherwise, cat command will look for the files in your present working directory.

2. Combine Two Files to New File

If you want to combine two or more files into a new single file, use > operator to redirect the output of above command to the new file as shown below. Here is an example to combine contents of two files into a new file file3.txt.

$ cat /home/file1.txt /home/file2.txt > /etc/data/file3.txt

In this case, the files file1.txt and file2.txt remain unchanged whereas file3.txt is completely overwritten. If the file does not exist, then cat command will create the new file afresh.

You can use this method to join any number of files. Just ensure that you add > operator after listing all files you want to concatenate.

$ sudo cat /home/file1.txt /home/file2.txt /home/file3.txt /home/file4.txt> /etc/data/file3.txt

3. Append Content to File

In the above approach, when you use > operator it completely overwrites the target file. If you only want to append the content of files to target file and not overwrite it, use >> operator instead.

$ cat /home/file1.txt /home/file2.txt >> /etc/data/file3.txt

You can use this method to join more than 2 files into a new file.

$ sudo cat /home/file1.txt /home/file2.txt /home/file3.txt /home/file4.txt >> /etc/data/file3.txt

In this article, we have learnt 3 different ways to merge files in Linux. You can customize them as per your requirement. You can use these methods to simply view the combined data of files, merge files and store the result in a new file, or merge files and append the result to another file.

Also read:

How to Rewrite URL to Another URL in Apache
How to Create PDF File in Python
How to Redirect IP to Domain in NGINX
How to Redirect IP to Domain in Apache
How to Fix Mixed Content/Insecure Content in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *