join – Join the lines with the same content in the specified field in the two files

The join command is a Linux utility that is used to join the lines of two files based on a common field. The command compares the specified field of each line in both files and joins the lines with the same content in the specified field. This command is useful when you need to combine data from two files into a single file based on a shared field.


The basic syntax of the join command is as follows:

join [options] file1 file2

The file1 and file2 arguments are the names of the files to be joined. By default, the join command uses the first field in each file as the field to be compared. If the files have different field separators, the -t option can be used to specify the field separator.

Here is an example of using the join command to combine two files based on a common field:

$ cat file1
1 apple
2 banana
3 orange

$ cat file2
1 red
2 yellow
3 orange

$ join file1 file2
1 apple red
2 banana yellow
3 orange orange

In this example, the first field of each file contains the same values, so the join command combines the lines based on that field. The output is a single file with three columns, where the first column is the common field and the second and third columns are the remaining fields from each file.

Specific use cases

The join command can be used in various situations, such as:

  • Merging data from two different sources based on a common field.
  • Comparing two different versions of a file and finding the differences.
  • Combining two files that contain related information, such as employee data and salary data.


The following table lists the available options for the join command:

Option Description
-a FILENUM Prints unpairable lines from FILENUM, where FILENUM is either 1 or 2.
-e EMPTY Replaces missing input fields with EMPTY.
-i Ignores case when comparing fields.
-j FIELD Joins the files based on the specified field number.
-o FORMAT Specifies the output format.
-t CHAR Uses CHAR as the field separator character.
-v FILENUM Prints only unpairable lines from FILENUM.

Troubleshooting tips

  • If the join command does not produce any output, it may be because the files do not have a common field. Make sure that the files have a shared field and that the field is correctly specified with the -j option.
  • If the output of the join command is not what you expected, check that the field separator is correctly specified with the -t option. By default, the field separator is a space character, but it can be changed to any other character using the -t option.


  • The join command only works with sorted files. If the files are not sorted, use the sort command to sort them before using join.
  • The join command can only join two files at a time. If you need to join more than two files, you can use the join command multiple times or use a scripting language like Python or Perl.