How to Append Contents of Multiple Files Into One File on Linux?

There are many situations where you may need to combine the contents of multiple files into one file. For example, you may have a number of log files that need to be analyzed or you may want to merge multiple text documents into one document for easy editing. On Linux, there are several ways to append the contents of multiple files into a single file, and in this article, we'll explore some of the most popular and effective methods.

Method 1: Using the cat Command

The cat command is the most common and straightforward tool for concatenating files. To append the contents of multiple files into a single file using cat, follow these steps:

  • Open a terminal and navigate to the directory containing your files

  • Use the ls command to list available files

  • Execute the cat command with the append operator

$ cat file1.txt file2.txt >> combined_file.txt

The >> operator appends the contents of file1.txt and file2.txt to the end of combined_file.txt, creating it if it doesn't exist. To combine more files, simply add their names to the command:

$ cat file1.txt file2.txt file3.txt >> combined_file.txt

You can also use wildcards to append multiple files matching a pattern:

$ cat *.txt >> combined_file.txt
$ cat log_*.txt >> all_logs.txt

Method 2: Using cat with Headers

When combining files, it's often useful to add headers to identify which content came from which file. This approach uses echo commands to add descriptive labels:

$ echo "=== Contents of file1.txt ===" >> combined_file.txt
$ cat file1.txt >> combined_file.txt
$ echo "" >> combined_file.txt
$ echo "=== Contents of file2.txt ===" >> combined_file.txt
$ cat file2.txt >> combined_file.txt

This method creates a well-organized combined file where each section is clearly labeled, making it easier to identify the source of specific content.

Method 3: Using the paste Command

The paste command merges files side by side rather than end-to-end. This is useful when you want to combine files column-wise:

$ paste file1.txt file2.txt > combined_file.txt

By default, paste separates columns with tab characters. You can specify a custom delimiter using the -d option:

$ paste -d "," file1.txt file2.txt > combined_file.txt
$ paste -d "|" file1.txt file2.txt file3.txt > combined_file.txt

Method 4: Using a for Loop

For more control over the appending process, you can use a for loop in bash. This method is particularly useful when you need to process files in a specific order or apply additional formatting:

$ for file in file1.txt file2.txt file3.txt; do
    echo "--- Start of $file ---" >> combined_file.txt
    cat "$file" >> combined_file.txt
    echo "--- End of $file ---" >> combined_file.txt
    echo "" >> combined_file.txt
done

This creates a well-structured output with clear delimiters between each file's content.

Comparison of Methods

Method Best Use Case Output Format Complexity
cat command Simple concatenation Sequential append Low
cat with headers Organized file sections Labeled sections Medium
paste command Column-wise merging Side-by-side columns Low
for loop Custom processing Flexible formatting High

Practical Example

Let's demonstrate with sample files. Create three test files:

$ echo -e "Line 1 from file A\nLine 2 from file A" > fileA.txt
$ echo -e "Line 1 from file B\nLine 2 from file B" > fileB.txt
$ echo -e "Line 1 from file C\nLine 2 from file C" > fileC.txt

Now append them using the cat method:

$ cat fileA.txt fileB.txt fileC.txt > merged.txt
$ cat merged.txt
Line 1 from file A
Line 2 from file A
Line 1 from file B
Line 2 from file B
Line 1 from file C
Line 2 from file C

Conclusion

Linux provides multiple powerful methods to append contents of multiple files into one file. The cat command is the most straightforward for simple concatenation, while paste works best for column-wise merging. For complex scenarios requiring custom formatting, bash loops offer maximum flexibility. Choose the method that best fits your specific requirements.

Updated on: 2026-03-17T09:01:38+05:30

35K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements