Bash Script to Merge Two Files Easily
Use the Bash command
cat file1 file2 > merged_file to merge two files by concatenating their contents into a new file.Examples
Inputfile1.txt content: Hello
file2.txt content: World
Outputmerged_file content: Hello
World
Inputfile1.txt content: Line1
Line2
file2.txt content: Line3
Line4
Outputmerged_file content: Line1
Line2
Line3
Line4
Inputfile1.txt content: (empty)
file2.txt content: Only this line
Outputmerged_file content: Only this line
How to Think About It
To merge two files, think of simply putting one file's content after the other. The
cat command reads files in order, and redirecting its output to a new file combines them. This is like stacking two pages one after another.Algorithm
1
Get the names of the two files to merge.2
Use a command to read the first file's content.3
Append the second file's content after the first.4
Redirect the combined content into a new file.5
Print a message confirming the merge.Code
bash
#!/bin/bash file1="$1" file2="$2" merged_file="merged_output.txt" cat "$file1" "$file2" > "$merged_file" echo "Files '$file1' and '$file2' merged into '$merged_file'."
Output
Files 'file1.txt' and 'file2.txt' merged into 'merged_output.txt'.
Dry Run
Let's trace merging 'file1.txt' with content 'Hello' and 'file2.txt' with content 'World'.
1
Read file1.txt
Content read: 'Hello'
2
Read file2.txt
Content read: 'World'
3
Concatenate and write to merged_output.txt
merged_output.txt content: 'Hello\nWorld'
| Step | Action | Result |
|---|---|---|
| 1 | Read file1.txt | Hello |
| 2 | Read file2.txt | World |
| 3 | Write merged_output.txt | Hello World |
Why This Works
Step 1: Using cat to read files
The cat command reads the contents of files in the order given.
Step 2: Redirecting output
Using > sends the combined output into a new file, creating or overwriting it.
Step 3: Merging files
By listing two files with cat, their contents are joined one after the other.
Alternative Approaches
Using tee command
bash
cat file1.txt file2.txt | tee merged_output.txt > /dev/null echo "Merged using tee."
This uses a pipe and tee to write output; slightly more complex but useful for simultaneous display.
Appending second file to first
bash
cp file1.txt merged_output.txt
cat file2.txt >> merged_output.txt
echo "Merged by appending."Copies first file then appends second; useful if you want to keep original files unchanged.
Complexity: O(n + m) time, O(1) space
Time Complexity
The time depends on reading both files fully, so it is proportional to the sum of their sizes (n and m).
Space Complexity
The script writes output directly to a file without extra memory, so space is constant.
Which Approach is Fastest?
Using cat file1 file2 > merged_file is fastest and simplest; alternatives add overhead with pipes or copying.
| Approach | Time | Space | Best For |
|---|---|---|---|
| cat with redirection | O(n + m) | O(1) | Simple and fast merging |
| tee with pipe | O(n + m) | O(1) | Merging with output display |
| copy then append | O(n + m) | O(1) | Preserving original files |
Always quote file names in scripts to handle spaces or special characters safely.
Forgetting to redirect output causes merged content to print on screen instead of saving to a file.