Linux paste Command: Join Corresponding Lines Side by Side

paste takes corresponding lines from multiple files and joins them side by side into a single line. The default delimiter is a tab, and it can also be used to turn vertically stacked values into a single line.

What is the paste command?

Unlike cat, which concatenates file contents vertically, paste matches the first lines together, the second lines together, and so on. If the file lengths differ, the shorter one is filled with empty fields.

Basic syntax

paste [option] [file...]

Installed implementations and options may vary depending on the distribution. Check the description on the current system with man paste.

Examples

Combine two files by columns

Attach the corresponding line from the second file next to each line of the first file.

paste names.txt scores.txt

Specify a delimiter

Use a colon instead of a tab to separate values from the two files.

paste -d ':' names.txt scores.txt

Changing one file into one row

-s reads the file continuously and joins the lines horizontally.

paste -s -d ',' names.txt

Combine lines of two inputs

This command places the first lines of two files side by side, and the second lines side by side, and so on. The example below does not create actual files because it uses process substitution in Bash.

paste -d, <(printf 'Ana\nBob\n') <(printf '7\n12\n')

Example output:

Ana,7
Bob,12

Unlike join, which matches rows by a common key, paste aligns lines by position. If one input has fewer lines, the missing fields are filled with empty values. In environments that do not support process substitution, such as POSIX-only shells, prepare the two inputs as temporary files.

Main Options and Format

Options/Format Description
-d list Specifies the delimiter to use at the joining position.
-s Combines lines from each file sequentially into one line.
- Uses standard input in place of file arguments.

Precautions when using

First, check whether the corresponding lines of two files refer to the same object. If the line order is different, incorrect values may be placed side by side without causing an error. If you need to join lines using a common key, check the sorting condition and then use join.

Frequently Asked Questions

What is the difference between paste and join?

paste attaches lines according to their line numbers, while join connects lines that have the same value in a common field.

Official Documentation

You can check the exact behavior of the options and the differences between implementations in the official documentation on paste.

More in This Category
Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux Tutorial / File Types and Metadata: ls, file, stat, and readlink

Linux Tutorial / File Types and Metadata: ls, file, stat, and readlink

Learn what ls, file, stat, and readlink each reveal about a Linux file, including type, size, timestamps, and symbolic-link targets.

Linux head Command: Display the Beginning of a File

Linux head Command: Display the Beginning of a File

Learn how Linux head prints the first lines or bytes of files and pipelines, labels multiple inputs, excludes a trailing portion, and handles multibyte data.

Linux rsync Command: Synchronize Files and Directories

Linux rsync Command: Synchronize Files and Directories

Learn how to synchronize Files and Directories with the Linux rsync command, including practical examples, key options, and important precautions.

Linux basename Command: Extract the Final Component of a Path

Linux basename Command: Extract the Final Component of a Path

Learn how to use the Linux basename command to remove directory prefixes and optional suffixes from path strings, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux modinfo Command: Inspect Kernel Module Details

Linux modinfo Command: Inspect Kernel Module Details

Learn how to inspect Kernel Module Details with the Linux modinfo command, including practical examples, key options, and important precautions.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.