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
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
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
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
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
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.
We can now confidently display or compare two files side by side in the Linux terminal.