Linux online course

Merge Files Line by Line with the Linux paste Command

Learn how to merge Linux files horizontally with paste, use custom delimiters, create serial output, handle unequal lengths, and safely redirect results.

The Linux paste command combines lines from files horizontally. It places corresponding lines side by side on the same output line, making it useful for pairing related values and creating simple delimited records.

This lesson assumes you can navigate directories, read text files, use command arguments, and understand basic shell quoting and redirection. See more Linux command-line topics if you need additional shell fundamentals.

What a horizontal file merge does

A horizontal merge combines related input lines side by side. The first line from one file is combined with the first line from another file, the second line with the second line, and so on.

This differs from vertical concatenation, which appends one file's contents after another. The cat command performs vertical concatenation:

cat names.txt departments.txt

For example, horizontal merging can pair names with departments, combine two simple columns, or create records such as user,role. It is appropriate when the records in separate files already correspond by line position.

The paste command

paste is the standard Linux utility for merging file contents by line position. Its basic syntax is:

paste file1 file2

You can supply more than two input files:

paste file1 file2 file3

In normal mode, paste reads the first line of every input file and writes them as one output line. It then reads the second line of every file, continuing in line-number order.

Example: names and departments

Suppose file_1 contains:

Alice
Bob
Carol

And file_2 contains:

Engineering
Support
Sales

Run:

paste file_1 file_2

The result has one name and one department on each line. The separator is a tab character by default.

Default delimiter behavior

A delimiter is a character placed between merged fields. By default, paste emits a tab character between fields.

paste file_1 file_2

Tabs may look like uneven spaces in a terminal. A tab advances to the next tab stop, so the visible gap depends on the length of the text before it. This does not mean that spaces were emitted: the output contains actual tab-separated fields.

Choosing a delimiter with -d

The -d option selects one or more delimiters:

paste -d DELIMITERS file1 file2

For example, use a slash between corresponding values:

paste -d '/' file_1 file_2

With values in file_1 of red, green, and blue, and values in file_2 of warm, cool, and cool, the output is:

red/warm
green/cool
blue/cool

Common delimiter choices

  • -d ',' for comma-separated records
  • -d ':' for colon-separated values
  • -d '|' for pipe-separated values
  • -d ' ' for a literal space
  • -d '\t' for a tab character supported by the paste implementation

Quote delimiters when the shell could interpret them. A pipe has a special meaning to the shell, so use -d '|' rather than an unquoted pipe. Quoting also makes spaces clear to the shell.

Delimiter lists with several files

When merging more than two files, a delimiter list supplies separators between successive fields. This command uses a colon between the first and second fields and a pipe between the second and third:

paste -d ':|' first.txt last.txt team.txt

If the files contain names, surnames, and teams, the output resembles:

Ada:Lovelace|Math
Linus:Torvalds|Kernel

If more separators are needed than the delimiter list contains, paste reuses the list. For example, with four input files and -d ',:', the separators cycle as comma, colon, comma.

Serial output with -s

The -s option switches from parallel processing to serial processing. Instead of combining equal-numbered lines from several files, it places all lines from one input file onto one output line.

paste -s -d ',' items.txt

If items.txt contains:

apple
banana
orange

The result is:

apple,banana,orange

Without -s, each input line would normally produce its own output line. With multiple files, serial mode processes each file separately and emits one line for each file:

paste -s -d ',' first.txt second.txt

The lines of first.txt become one delimited line, followed by a separate delimited line containing the lines of second.txt. The delimiter list is applied between values within each serial output line.

paste options used for merging files

No option: Merge corresponding lines with tab separators.

-d DELIMITERS: Choose one or more output separators, such as comma, colon, or pipe.

-s: Process each file serially, placing its lines on one output line.

Files with unequal line counts

Input files do not have to contain the same number of lines. When one file runs out of lines, paste supplies an empty field for that file while continuing to read available lines from the other files.

For example, if left.txt contains:

A
B
C

And right.txt contains:

1
2

Run:

paste -d '|' left.txt right.txt

The result is:

A|1
B|2
C|

The final line has an empty right-side field. This behavior can be useful, but it may also indicate a data problem. Validate related datasets before merging when every record is expected to have a matching value.

wc -l file_1 file_2

Compare the reported line counts and decide whether unmatched lines should be corrected, removed, or intentionally preserved as empty fields.

Saving merged output safely

By default, paste writes to standard output, the normal output stream for a command. Shell redirection saves that output to a file:

paste -d ',' users.txt roles.txt > users-with-roles.csv

This creates a new comma-delimited file such as:

u100,admin
u101,viewer

Do not redirect directly to one of the input files:

paste file_1 file_2 > file_1

The shell opens the output file before paste starts reading input. Opening it for writing can truncate file_1, causing data loss or an empty result.

Write to a different temporary file, inspect it, and replace the destination only when the result is correct:

paste -d ',' file_1 file_2 > merged.tmp && mv merged.tmp merged.txt

For extra safety, keep backups of important source files and verify the temporary output with a command such as cat merged.tmp before moving it.

paste compared with related commands

The correct command depends on how records should be combined:

Append complete files one after another: Use cat. Records are combined vertically.

Combine matching line numbers side by side: Use paste. Records are combined horizontally by position.

Combine records that share a key field: Use join. Records are matched by a common field rather than by line number.

For example, if one file contains a record for Alice on line 1 and another file contains Alice's department on line 4, paste will not find that relationship. It pairs line 1 with line 1. Use a key-based approach such as join, often after sorting the input files as required by that command.

When the goal is visual presentation in aligned columns rather than creating raw delimited records, a column-formatting tool may be more appropriate. Tabs are data separators; displayed column alignment can require additional formatting.

Practical command patterns

Merge two files with tabs

paste file_1 file_2

Merge two files with a slash

paste -d '/' file_1 file_2

Create and save comma-separated records

paste -d ',' users.txt roles.txt > users-with-roles.csv

Merge three files with different separators

paste -d ':|' first.txt last.txt team.txt

Flatten one file into a single line

paste -s file_1
paste -s -d ',' items.txt

Use a space or tab explicitly

paste -d ' ' file_1 file_2
paste -d '\t' file_1 file_2

Troubleshooting paste output

The columns do not look aligned

The default separator is a tab, and tab stops depend on the length of preceding text. Use an explicit delimiter for machine-readable output. If fixed visual columns are required, pass the result through a formatting tool instead of treating terminal appearance as evidence about the stored separators.

Blank values appear near the end

The input files probably have different line counts. Check them with wc -l file_1 file_2. Then correct the datasets, remove unmatched records, or accept the empty fields deliberately.

A pipe delimiter behaves unexpectedly

The shell uses an unquoted pipe as a command pipeline operator. Quote it:

paste -d '|' left.txt right.txt

An input file became empty

This usually happens when output was redirected to an input filename. The shell truncates the destination before the command reads it. Use a separate temporary output file, inspect it, and then rename it if appropriate.

Records are paired incorrectly

paste matches by line number only. It does not inspect names, IDs, or other fields. If records must match by an identifier, use a key-based tool such as join or a suitable awk workflow.

Serial mode produces an unexpected layout

-s changes the operation from side-by-side merging to one-file-at-a-time processing. Remove -s for normal parallel merging, and keep it when the goal is to place each file's lines onto a single output line.

Exam-relevant notes

  • paste performs a horizontal merge by line position.
  • cat performs vertical concatenation by appending file contents.
  • The default paste delimiter is a tab character.
  • -d selects a delimiter or cycles through a delimiter list.
  • -s processes each file serially instead of merging corresponding lines in parallel.
  • Unequal input lengths can create empty fields.
  • Use quoted delimiters when shell metacharacters, such as |, are involved.
  • Never redirect output directly to an input file; write to a separate file first.
  • Use join when records must be matched by a shared field rather than line number.