How to Merge Files Using the join Command in Linux

If you are managing databases or structured text files in a Linux terminal, you frequently need to combine data from two different sources. While the paste command simply slams two files together side-by-side regardless of their content, the join command is significantly more intelligent. Modeled after the SQL JOIN operator, this utility merges lines from two distinct files based exclusively on a shared, common field (like a user ID or an email address).

Understanding the Prerequisites

The join command is extremely powerful, but it has one strict, absolute requirement: Both files must be sorted based on the exact field you intend to join them on. If the files are not sorted, join will fail silently or skip lines.

Imagine you have two files. users.txt contains an ID number and a name. salaries.txt contains the exact same ID number and a salary. Because the ID number exists in both files, we will use it as the “common field.”

First, guarantee both files are sorted numerically:

sort -n users.txt > sorted_users.txt
sort -n salaries.txt > sorted_salaries.txt

How to Join Two Files

By default, join assumes that the very first column (field 1) in both files is the common field, and it assumes the columns are separated by whitespace.

To merge our two sorted files based on that first ID column, run:

join sorted_users.txt sorted_salaries.txt

If sorted_users.txt contains 101 Alice and sorted_salaries.txt contains 101 $75000, the resulting output will be a perfectly merged line: 101 Alice $75000. Notice that join is smart enough to only print the shared ID number once.

How to Specify Different Delimiters and Fields

If you are working with CSV files (Comma-Separated Values), you must explicitly tell join to use a comma instead of whitespace using the -t flag.

join -t ',' sorted_users.csv sorted_salaries.csv

What if the common ID number is the first column in the users file, but the second column in the salaries file? You can specify exactly which field to use for each file using the -1 (file one) and -2 (file two) flags.

join -1 1 -2 2 sorted_users.txt sorted_salaries.txt

This command instructs Linux to find the match by comparing the first column of the first file directly against the second column of the second file.

Get the best tech tips delivered straight to your inbox.

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