paste Command in Linux: Merge Lines from Files

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:
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:
Iron Man
Thor
Captain America
Hulk
Spider ManBlack Widow
Captain Marvel
Dark Phoenix
NebulaWhen invoked without an option, paste reads all files given as arguments and horizontally merges the corresponding lines, separated by a tab:
paste file1 file2Iron Man Black Widow
Thor Captain Marvel
Captain America Dark Phoenix
Hulk Nebula
Spider ManInstead of displaying the output on the screen, you can redirect it to a file using the > or >> operators:
paste file1 file2 > file3If 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:
paste -d '_' file1 file2Iron Man_Black Widow
Thor_Captain Marvel
Captain America_Dark Phoenix
Hulk_Nebula
Spider Man_Here is an example of using two delimiters:
paste -d '%|' file1 file2 file1The 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.
Iron Man%Black Widow|Iron Man
Thor%Captain Marvel|Thor
Captain America%Dark Phoenix|Captain America
Hulk%Nebula|Hulk
Spider Man%|Spider ManThe -s, --serial option tells paste to display the lines of one file at a time instead of one line from each file.
paste -s file1 file2The command merges all lines from each given file into separate lines:
Iron Man Thor Captain America Hulk Spider Man
Black Widow Captain Marvel Dark Phoenix NebulaWhen 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
| Command | Description |
|---|---|
paste file1 file2 | Merge files side by side (tab-separated) |
paste -d ',' file1 file2 | Use a custom delimiter |
| `paste -d ‘% | ’ file1 file2 file3` |
paste -s file1 | Merge all lines of a file into one line |
paste -s file1 file2 | Serialize each file onto its own line |
paste -z file1 file2 | Use NUL as line delimiter |
paste - - < file | Merge every two lines of a file side by side |
paste file1 file2 > out.txt | Redirect 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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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