Linux paste Command


The paste command in Linux is used to merge lines of files horizontally. It essentially combines corresponding lines from multiple files or inputs, separating them by a specified delimiter (default is a tab).

Basic Syntax

paste [options] file1 file2 ...

Common Options

  • -d : Specifies a delimiter to use instead of the default tab.
  • -s : Merges all lines of each file into a single line, sequentially.

Examples with Output

Example 1: Merging Two Files Line by Line

Suppose you have two files, file1.txt and file2.txt:

file1.txt

apple banana cherry

file2.txt

red yellow purple
Command
paste file1.txt file2.txt
Output
apple red banana yellow cherry purple

Explanation:

  • paste merges each line from file1.txt with the corresponding line from file2.txt using a tab separator.

Example 2: Using a Custom Delimiter

To specify a different delimiter, use the -d option.

Command
paste -d '-' file1.txt file2.txt
Output
apple-red banana-yellow cherry-purple

Explanation:

  • The -d '-' option sets the delimiter to a hyphen instead of a tab, creating apple-red instead of apple red.

Example 3: Merging Multiple Files

You can combine more than two files at once with paste.

Suppose you have file3.txt:

file3.txt

fruit color flavor
Command
paste file1.txt file2.txt file3.txt
Output
apple red fruit banana yellow color cherry purple flavor

Explanation:

  • paste merges each line from file1.txt, file2.txt, and file3.txt into a single line separated by tabs.

Example 4: Sequentially Merging Lines Using -s

The -s option allows you to merge all lines from each file into a single line.

Command
paste -s file1.txt
Output
apple banana cherry

Explanation:

  • paste -s file1.txt takes all lines from file1.txt and pastes them into a single line.

Example 5: Using Different Delimiters for Each File

With the -d option, you can specify multiple delimiters, and they will be applied in sequence across files.

Command
paste -d ':-+' file1.txt file2.txt file3.txt
Output
apple:red-fruit banana:yellow-color cherry:purple-flavor

Explanation:

  • -d ':-+' uses : as the delimiter between file1.txt and file2.txt, - between file2.txt and file3.txt, and then repeats.

Summary of Common paste Commands

CommandDescription
paste file1 file2Merge lines from file1 and file2 line by line.
paste -d ',' file1 file2Use a comma , as the delimiter between merged lines.
paste -s file1Combine all lines from file1 into a single line.
paste -d ':-' file1 file2 file3Use different delimiters between files (repeating if needed).

The paste command is especially helpful for creating side-by-side file views, data manipulation, or merging columns from different sources in Linux.