paste Command in Linux: Merge Lines from Files

By 

Updated on

4 min read

paste command merging lines from two files in Linux

paste merges lines from files horizontally. It outputs lines made from the corresponding lines of each input file, separated by tabs by default.

This article explains how to use the paste command in Linux.

How to Use the paste Command

paste is one of the lesser-known Linux and Unix command-line utilities.

The general syntax for the paste command is as follows:

txt
paste [OPTION]... [FILE]...

If no input files are provided or when - is given as argument, paste uses the standard input.

Suppose we have the following files:

file1txt
Iron Man
Thor
Captain America
Hulk
Spider Man
file2txt
Black Widow
Captain Marvel
Dark Phoenix
Nebula

When invoked without an option, paste reads all files given as arguments and horizontally merges the corresponding lines, separated by a tab:

Terminal
paste file1 file2
output
Iron Man	Black Widow
Thor	Captain Marvel
Captain America	Dark Phoenix
Hulk	Nebula
Spider Man

Instead of displaying the output on the screen, you can redirect it to a file using the > or >> operators:

Terminal
paste file1 file2 > file3

If the file does not exist, it will be created. The > operator will overwrite an existing file, while the >> operator will append the output to the file.

The -d, --delimiters option allows you to specify a list of characters to be used as delimiters instead of the default TAB separator.

Each delimiter is consecutively used. When the list is exhausted, paste starts again from the first delimiter character.

To use the _ (underscore) character as a delimiter instead of TAB, you would type:

Terminal
paste -d '_' file1 file2
output
Iron Man_Black Widow
Thor_Captain Marvel
Captain America_Dark Phoenix
Hulk_Nebula
Spider Man_

Here is an example of using two delimiters:

Terminal
paste -d '%|' file1 file2 file1

The lines from the first and the second file are separated with the first character from the delimiters list. The second and the third file lines are separated with the second delimiter.

If more files were given, paste starts again from the beginning of the list.

output
Iron Man%Black Widow|Iron Man
Thor%Captain Marvel|Thor
Captain America%Dark Phoenix|Captain America
Hulk%Nebula|Hulk
Spider Man%|Spider Man

The -s, --serial option tells paste to display the lines of one file at a time instead of one line from each file.

Terminal
paste -s file1 file2

The command merges all lines from each given file into separate lines:

output
Iron Man	Thor	Captain America	Hulk	Spider Man
Black Widow	Captain Marvel	Dark Phoenix	Nebula

When used with the -z, --zero-terminated option, paste uses a null character to delimit the items instead of the default newline character. This behavior is handy when paste is used in combination with find -print0 and xargs -0 commands to handle file names containing special characters.

Quick Reference

CommandDescription
paste file1 file2Merge files side by side (tab-separated)
paste -d ',' file1 file2Use a custom delimiter
`paste -d ‘%’ file1 file2 file3`
paste -s file1Merge all lines of a file into one line
paste -s file1 file2Serialize each file onto its own line
paste -z file1 file2Use NUL as line delimiter
paste - - < fileMerge every two lines of a file side by side
paste file1 file2 > out.txtRedirect merged output to a file

FAQ

What is the difference between paste and cat?
cat concatenates files vertically — it appends one file after another. paste merges files horizontally, combining corresponding lines from each file side by side.

How do I merge every two lines of a single file?
Use - as a file argument twice to read from stdin twice: paste - - < file. Each pair of consecutive lines is merged onto one output line separated by a tab.

Can I use a comma as a delimiter to create a CSV?
Yes. Run paste -d ',' file1 file2 to produce comma-separated output. You can then redirect it to a .csv file with paste -d ',' file1 file2 > output.csv.

What happens when the files have different numbers of lines?
paste continues until all files are exhausted. Lines from the shorter file are replaced with an empty field, so the delimiter still appears but with nothing after it — as shown in the Spider Man_ example above.

Conclusion

The paste command is a simple and efficient way to merge corresponding lines from multiple files. Combine it with -d to control the separator and -s to serialize file contents onto single lines.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page