Home Linux Commandline Tips How to Display Two Files Side by Side in Linux

How to Display Two Files Side by Side in Linux

File management is an important aspect of Linux administration, therefore, learning a few tricks to lessen the hurdles involved in working with user or system files under the Linux ecosystem is always welcomed.

One of these tricks is how you choose to display your files. Instead of opting for a graphical file reader and using your computer touchpad or mouse to move/navigate from one file to another, you could opt to remain in your command line environment and have a preview of the two files you wish to compare side by side.

Problem Statement

For this tutorial article, we will need to create two text files to reference their side-by-side view on the Linux terminal environment.

$ sudo nano file_1.txt
$ sudo nano file_2.txt 
Create Files in Linux
Create Files in Linux

Now that we have the two sample files to view from the Linux terminal, we can start looking at the Linux commands/utilities needed.

1. Using pr Command

The pr command is part of the GNU Coreutils package hence its inclusion in almost all Linux operating system distributions. This command is primarily applicable in paginating text files. However, it is extensive enough to be used for the optional display of files side by side.

The pr command’s syntax is as follows:

$ pr [option]… [file1]…[file2]

The [option] portion of the command syntax includes -m for merging and printing the files in parallel and -t to omit any pagination. We can view our two files using the pr command in the following manner:

$ pr -m -t file_1.txt file_2.txt 
View Files Side by Side in Linux
View Files Side by Side in Linux

To increase the page width so that the files’ views can be distinctive use -w option and specify a value.

$ pr -m -t -w 120 file_1.txt file_2.txt 
Increase File View Width in Linux
Increase File View Width in Linux

2. Using sdiff Command

The sdiff command is primarily used for two files comparison with the objective of displaying their differences. It is a member of the Diffutils package makes it an ideal candidate for displaying two files side by side.

The sdiff command’s syntax is as follows:

$ sdiff [option]… [file1]…[file2]

We can view our two files in the following manner:

$ sdiff file_1.txt file_2.txt 
Compare Two Files Side by Side
Compare Two Files Side by Side

The vertical bars (|) signify the lines on both files that do not match. For instance, the line Update Commands: is a match on both files and therefore missing a vertical bar (|).

3. Using paste Command

The paste command is a member of the GNU utils package and also makes it available in almost all Linux operating system distributions. It merges two files by creating their parallel views.

The paste command’s general syntax is as follows:

$ paste [option]… [file1]… [file2]…

The view of our two files using the paste command is as follows:

$ paste file_1.txt file_2.txt   
View Two Files Side by Side
View Two Files Side by Side

The output from the paste command is not formatted hence lacking clarity. To fix this issue, we need to use the column command from the bsdmainutils package.

It should be available in almost all Linux OS distributions. Our new command will now look like the following after we pipe the paste command to the column command:

$ paste file_1.txt file_2.txt | column -t -s $'\t'

The -t and -s options in the column command are for creating columns and specifying delimiter tab characters respectfully.

View Files Side by Side in Linux
View Files Side by Side in Linux

We can now confidently display or compare two files side by side in the Linux terminal.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.