Linux join Command: Combine Files by a Common Field

join combines rows with the same key value from two text files into a single row. The difference from paste is that it joins based on field values, not row numbers.

What is the join command?

By default, it uses the first field of each file as the key, and both files must be sorted by that key. The default behavior is to output only matching rows. To check for missing keys as well, review the -a or -v options.

Basic syntax

join [option] file1 file2

Installed implementations and options may vary depending on the distribution. Check your system's description with man join.

Examples

Join by the first field

After sorting the two files according to the same rule, they are joined.

join names.sorted scores.sorted

Specify a delimiter

Colon-separated files must use the same delimiter system in both inputs.

join -t ':' users.sorted roles.sorted

Display unmatched rows from the first file

If you want to see unmatched rows, add -a 1.

join -a 1 names.sorted scores.sorted

Result of joining on common fields

join basically merges rows where the first field of each file is the same. The following files are pre-sorted based on the first column, which is the join key.

printf 'a Ana\nb Bob\n' > names.txt
printf 'a 7\nb 12\n' > ages.txt
join names.txt ages.txt

Example output:

a Ana 7
b Bob 12

Input files should be sorted according to the same locale and key. Rows that do not match are omitted from the default output, so to check for missing keys, review -a 1 or -a 2. The two files in the example are created in the current directory, so after the work is done, check if they are needed and clean up.

Main Options and Format

Options/Format Description
-t character Specifies the input/output field delimiter.
-1 N / -2 N Specifies the merge key fields for the first and second files.
-a 1 or -a 2 Also outputs unmatched rows from the specified file.
-v 1 or -v 2 Outputs only unmatched rows from the specified file.
-o format Specifies the output fields and order.

Precautions when using

Both files must be sorted by the same key, delimiter, and locale. Unsorted input may result in missing or incorrectly combined entries. If a key is duplicated, multiple output lines can be produced for a single key, so check the resulting count.

Frequently Asked Questions

Can an unsorted file be joined directly?

The usual join operation requires both files to be sorted by the joining field. Make a copy with the sorting order and locale matched before running.

Official Documentation

The exact behavior of the options and differences between implementations can be found in the official documentation for join.

More in This Category
Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux mktemp Command: Create Secure Temporary Files and Directories

Linux mktemp Command: Create Secure Temporary Files and Directories

Learn how to create unpredictable temporary files and directories with Linux mktemp, use templates and TMPDIR, clean up with trap, and avoid unsafe dry runs.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux du Command: Check File and Directory Disk Usage

Linux du Command: Check File and Directory Disk Usage

Learn how to use the Linux du command to measure and summarize disk space used by files and directories, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ln Command: Create Hard Links and Symbolic Links

Linux ln Command: Create Hard Links and Symbolic Links

Learn how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux id Command: Show User and Group IDs

Linux id Command: Show User and Group IDs

Learn how to show User and Group IDs with the Linux id command, including practical examples, key options, and important precautions.