Concatenate Files in Linux: cat and Beyond
To concatenate files in Linux, you use the cat command followed by the file names, like cat file1.txt file2.txt > combined.txt. This joins the files in the order you list them and writes the result to standard output or a new file. Below we explain the cat command, its options, alternatives, and common pitfalls.
What Is Concatenation?
Concatenation means joining two or more files end-to-end into a single stream or file. In Linux, this is a fundamental operation used to merge text files, combine logs, or prepare data for further processing. The cat command (short for concatenate) is the standard tool for this job, and it has been part of Unix since the early days. It reads each file in sequence and writes the contents to standard output, which you can redirect to a file or pipe to another command.
Basic cat Syntax and Examples
The simplest form is:
cat file1 file2 file3
This prints the contents of all three files to the terminal. To save the combined output to a new file, use redirection:
cat file1 file2 > combined.txt
If you want to append to an existing file instead of overwriting it, use >>:
cat file1 file2 >> existing.txt
You can also concatenate files with different names, including files in different directories. For example:
cat /var/log/syslog /var/log/auth.log > all_logs.txt
If you need to add a header or separator between files, you can use the -n option to number lines, or use awk or sed for more control. But for simple joining, cat is enough.
Important cat Options
-n: number all output lines (including blank lines).-b: number only non-empty lines.-s: squeeze multiple consecutive blank lines into one.-E: show$at the end of each line (useful for spotting trailing spaces).-A: equivalent to-vET, showing all non-printing characters.
For concatenation, the most useful are -n and -b when you need line numbers in the combined output. For example:
cat -n file1 file2 > numbered.txt
This will number lines sequentially across both files.
How to Choose the Right Method
While cat is the go-to for simple concatenation, there are situations where other tools are better:
- Huge files: If you are combining very large files (gigabytes),
catis fine because it streams, but you may want to considerddfor raw block copying ortarfor archiving multiple files with metadata. - Binary files:
catworks with binary data too, but be careful with redirection and terminal output. For binary files, usecat file1 file2 > combined.bin– it will work, but you cannot view the output on the terminal. - Adding separators: If you need a line like “=== FILE 1 ===” between files,
catalone cannot do that. Use a loop orawk. - Sorting or processing: If you need to sort the combined content, use
sort file1 file2directly, or pipecatintosort.
Common Mistakes and How to Avoid Them
The most common mistake is using a single > when you meant to append with >>. This will overwrite the destination file, which can be disastrous if you accidentally overwrite a source file. For example:
cat file1 file2 > file1 # WRONG! This truncates file1 before reading it.
This command will empty file1 because the shell creates the output file before cat reads the input. Always use a different output file name, or if you must overwrite, use a temporary file and then move it.
Another mistake is forgetting that cat does not add any separator between files. If you concatenate two files where the first does not end with a newline, the last line of the first file and the first line of the second will be joined. To avoid this, ensure files end with a newline, or use awk to add one.
Alternatives to cat for Concatenation
Several other commands can concatenate files, each with specific strengths:
- tac: Reverses the order of lines within each file, but you can use it to concatenate in reverse order:
tac file2 file1(note thattacreverses each file’s lines, so it is not a direct concatenation tool). - paste: Joins files column-wise, not line-wise. Useful for merging files side-by-side.
- awk: Can concatenate with custom separators:
awk '1' file1 file2is equivalent tocat, but you can add headers:awk 'FNR==1{print "--- " FILENAME " ---"}1' file1 file2. - dd: For binary files,
dd if=file1 of=combined bs=1M conv=notrunccan be used, but it is more complex and rarely needed. - zcat: For compressed files,
zcat file1.gz file2.gzconcatenates the decompressed content.
Frequently Asked Questions
How do I concatenate files and add a newline between them?
Use a loop or awk. For example: for f in file1 file2; do cat "$f"; echo; done > combined.txt. Or with awk: awk '1' file1 file2 will not add a newline if the last line of each file lacks one, but you can force one with awk 'FNR==1 && NR!=1{print ""}1' file1 file2.
Can I concatenate files with different encodings?
Yes, cat does not care about encoding; it just copies bytes. However, the resulting file will have mixed encodings, which may cause display issues. It is better to convert files to a common encoding first using iconv.
What is the difference between cat and cp?
cp copies a single file to a new location, preserving its content and often its permissions. cat concatenates multiple files into a single stream, which you can redirect to a file. To copy one file, cp is more efficient and preserves metadata.
How do I concatenate files and compress the result on the fly?
Pipe cat into a compression tool: cat file1 file2 | gzip > combined.gz. Or use tar to archive multiple files with compression: tar czf combined.tar.gz file1 file2.
Why does cat show binary garbage on the terminal?
If you run cat on a binary file without redirection, it sends the raw bytes to the terminal, which interprets them as control characters, causing garbled output. Use cat -v to display non-printing characters safely, or redirect to a file.
Can I concatenate files from a wildcard pattern?
Yes, the shell expands wildcards: cat *.txt > all.txt. The order is alphabetical by default. If you need a specific order, list the files explicitly or use sort to control the expansion.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.