How to Concatenate Files in Linux

How to Concatenate Files in Linux

Putting files together in Linux means choosing how their contents should appear in the result. The cat command copies each file’s contents into one stream, in the order you name them.

You’ll see how to combine files with cat and use redirection to control the destination.

TL;DR: Concatenate files with cat

Use cat with the source files in the order you want, then redirect its output to a separate destination.

cat part-a.txt part-b.txt > combined.txt
  • Use > to create or replace the destination.
  • Use >> when you mean to add the stream to an existing destination.
  • Use paste when matching lines should become columns.

What does concatenating files mean?

The cat command copies each named input to standard output in operand order, producing one stream.

Short report sections and log extracts are typical text inputs. The command adds no separator between them, so if a file lacks a final newline, the next starts at its last byte.

A reader on Unix & Linux Stack Exchange asked for “file1 1st line followed by file2 first line, and file1 second line followed by file2 second line etc.” The comparison below shows which command fits each layout.

Choose the utility from the data relationship. If records need matching by an identifier rather than row position, prepare the inputs for a field-aware utility such as join.

Desired resultCommandHow it arranges content
Sections stacked in a streamcatWrites one entire input after another.
Corresponding lines in columnspasteCombines line one with line one, then line two with line two.
Records matched by a keyjoinCombines rows using a shared field.

How to concatenate files in Linux step by step

This workflow uses sample text files so each source and result is clear.

Step 1: List the inputs in the required order

A dash in the input list represents standard input, which lets a pipeline contribute one part of the stream.

#!/usr/bin/env bash
set -eu
printf 'north\neast\n' > part-a.txt
printf 'south\nwest\n' > part-b.txt
cat part-a.txt part-b.txt > combined.txt
cat combined.txt

Save the script as merge_demo.sh, then run bash merge_demo.sh. I ran that command and got this output:

bash merge_demo.sh
north
east
south
west
Terminal output showing files concatenated in sequence
cat prints the first input, followed by the second input.

The terminal returns after west and reports exit 0, confirming the script completed.

Step 2: Save the combined stream to a new file

Redirect standard output with > when the destination should contain only this result. The shell creates a missing destination or truncates an existing one before cat begins, so select a new name or confirm replacement is intended.

cat part-a.txt part-b.txt > combined.txt
cat combined.txt

Read the destination to verify the sequence. The cat guide covers its other display options.

Step 3: Append instead of replacing

Use >> for a batch that belongs after existing destination content. It creates the destination when it is missing.

cat part-a.txt part-b.txt >> combined.txt

Each invocation adds the named inputs again. Keep the destination separate from the inputs because the shell opens it before cat starts.

Step 4: Expand a set of matching filenames

A shell glob stands in for a long list of similarly named files. The shell expands an unquoted asterisk before cat runs, while a quoted wildcard stays literal.

cat reports/part-*.txt > combined.txt

Check the match list because a broad *.txt glob may include an earlier destination. A shell for loop is another way to process a reusable sequence of filenames.

Step 5: Join corresponding lines side by side

The -d option selects a delimiter such as a comma for plain fields. Corresponding input lines become columns.

paste -d ',' names.txt emails.txt > contacts.csv

For unequal files, later rows in the longer file have empty columns where the shorter input has ended. The -s option places each file’s lines in a serial row. Decide whether those empty cells mean missing values or expected blanks.

Quoted commas need a CSV-aware parser. When several columns are present, paste cycles through the delimiter characters supplied to -d.

Edge cases when combining files

I ran bash missing_input_demo.sh and got this diagnostic:

cat: absent-part.txt: No such file or directory
Terminal showing cat error for a missing input file
cat reports the path it could not open and exits with status 1.

The named operand is absent, so the command exits nonzero. Correct the path or restore the file before relying on any output.

CaseWhat happensWhat to do
Destination equals an input with >The shell truncates the destination before cat reads it.Choose a distinct output filename.
Glob matches old outputThe prior result may be included as another source.List matches and keep output outside the source set.
Input lacks a final newlineThe next input’s first bytes continue on the same line.Add a delimiter deliberately if the format requires one.
Rows should align across filescat stacks complete streams, not matching rows.Use paste and select its delimiter.

Text files can have different encodings or line endings, and a program reading the result may interpret those differences in unexpected ways. Check source formats when downstream software expects a particular encoding or newline convention.

cat copies bytes. It cannot combine the internal structures of spreadsheets, compressed archives, or images. Those formats need their own utilities.

The Linux split command guide covers dividing a file into parts. cmp checks whether outputs match byte for byte.

Conclusion

Match the command to the data: cat keeps file sections in sequence, while paste aligns entries by line.

Verify filenames and inspect the result before using it. GNU documents cat and paste. LinuxForDevices explains how to create files with touch.

FAQ

These answers distinguish the common output and ordering choices.

How do I concatenate two files in Linux?

Run cat first.txt second.txt > combined.txt. The order of the input paths determines the order in the result, and > replaces an existing destination.

How do I append multiple files to an existing file?

Use cat first.txt second.txt >> combined.txt. The >> operator appends the command output instead of truncating the existing destination.

How do I merge files line by line?

Use paste file-a.txt file-b.txt to join corresponding lines into tab-separated columns. Add -d with a delimiter when the output needs a different separator.