Since cat stands for concatenate (which means "link together"), combining files is one of its primary jobs!
To combine files, you use the > (redirection) operator. This operator takes the output that would normally show up on your screen and sends it into a file instead.
Here are the most common ways to do it:
1. Combine two files into a new third file
If you have file1.txt and file2.txt and want to merge them into combined.txt:
cat file1.txt file2.txt > combined.txt
The order matters: file1.txt will appear first in the new file.
2. Append one file to the end of another
If you want to take the contents of file2.txt and add them to the bottom of file1.txt (without creating a new file), use >>:
cat file2.txt >> file1.txt
Note: Using > (single) overwrites the file, while >> (double) appends to the end.
3. Combine many files using a wildcard
If you have a dozen text files and want to merge them all:
cat *.txt > all_files_merged.txt
Try it yourself!
While you are in the terminal, you can try this quick test:
echo "Hello" > part1.txtecho "World" > part2.txtcat part1.txt part2.txt > full.txtcat full.txt
You'll see that full.txt now contains both words! Does that help explain how it works?