How to Merge Files Horizontally Using the paste Command in Linux

When you are managing a Linux database, you often encounter data spread across multiple separate text files. For example, first_names.txt contains a vertical list of 500 names, and last_names.txt contains a vertical list of 500 surnames. If you use the standard cat command to merge them, the system will simply stack them vertically (500 first names followed by 500 last names), creating a useless 1,000-line file. To mathematically force the Linux kernel to stitch two files together horizontally, side-by-side, generating a perfect two-column layout, you must use the paste command.

How the paste Command Works

The paste command is a structural alignment engine. It takes the absolute first line of File A, grabs the absolute first line of File B, glues them together side-by-side (separated by a standard Tab character), and outputs them as a single, unified line. It then repeats this process sequentially until it hits the bottom of the files.

To execute a basic horizontal merge, type the command followed by your target files:

paste first_names.txt last_names.txt

The terminal will instantly output a perfect, tabular structure:

John    Smith
Sarah   Connor
David   Bowman

Like most Linux text manipulation tools, this only prints to your screen. To permanently lock this two-column layout into a brand new file, use the redirect operator:

paste first_names.txt last_names.txt > full_names.txt

Modifying the Delimiter

By default, the paste command forces a massive, invisible Tab character between the two merged columns. If you are preparing this data to be imported into Microsoft Excel or a SQL database, a Tab is often the wrong structural delimiter. You need a standard Comma-Separated Values (CSV) format.

To override the default Tab, you must use the -d (delimiter) flag followed by your specific character (in quotes).

paste -d "," first_names.txt last_names.txt > full_names.csv

The engine will now instantly rip out the Tab spacing and inject a mathematically perfect comma between every single merged line (e.g., John,Smith), generating a perfectly formatted file ready for corporate database ingestion.

You can use the -d flag to inject any character you want. If you are generating a list of automated email addresses, you can use the @ symbol as a delimiter:

paste -d "@" usernames.txt domains.txt

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.