Compare Two Text Files Side By Side

Today’s article has a theme that may seem familiar, as I recently wrote an article about how to compare two text files side by side. That article was well received and I figured I’d share another way to compare two text files side by side. After all, this is Linux and there are usually many paths to the same destination.

You can use a variety of tools to compare files. It’s not hard to check two files to see the differences between them. However, you can visually compare them quite easily. All you need to do is to show those two files in a side-by-side manner.

The previous article can be read here:

Show Two Text Files Side By Side

So, that’s one way to show two text files side by side. This article is going to show you another way. Why? Because I can! Also, I’d like to write this article while it’s still fresh in my memory. If I don’t write it soon, I’ll have forgotten to write it at all. Age will do that to a person. It is what it is.

Preparation:

In the previous article, I had you download two files. I’m going to suggest that you do so again. That way, we’re on the same page. 

Download these two files to your ~/Documents directory:

File One
File Two

Those two files should be the same in every way. Byte for byte, they should be the same (with maybe a slight difference in metadata like last accessed time).

The tool we’ll be using is sdiff and you almost certainly have this installed by default. It’s part of the diff tools, so it is likely installed by default. You can verify that sdiff is previously installed with the following command:

 The output should show you that sdiff is just a part of the diff utilities. It should say something similar to this:

sdiff (GNU diffutils) 3.8

Those are the only tools you’ll need. Well, that and a terminal emulator of your choosing. It’ll be easy enough for anyone to follow along with this article.

Compare Two Text Files Side By Side:

As I mentioned above, you’ll need an open terminal. For most distros, you can just press CTRL + ALT + T and your default terminal emulator should open. If not, open a terminal from your application menu.

If you followed the above directions and downloaded the two files, you will first need to change your directory to your ~/Documents directory. That’s easily done with this command:

Then, you can simply compare the two files. The syntax is like this:

So, in this case, the command would look like this:

As you can see the files are the same.

using sdiff to compare two text files side by side
See? The files are the same. You can use sdiff to compare two text files side by side.

Let’s mix things up…

Change One File:

We already have the terminal open, so let’s see this sdiff in action with a changed file. Let’s edit the sort1.txt file with Nano. Run this command:

Now, scroll to the bottom and add the following line:

Then, you need to save the updated sort1.txt file. As we’re using Nano, you can do that by pressing CTRL + X, then Y, and then ENTER. It’s not hard to save a file in Nano.

Now, let’s run the sdiff command a second time so that we can compare two different text files side by side. 

You’ll see a different output. It should look like this:

comparing two different files with sdiff
You can see that one line is different when you compare the two text files.

The > character indicates that there’s a difference. Specifically, it indicates that there’s a new line that isn’t in the first text file. 

On the other hand…

comparing two files with sdiff and one has a different line
This time, the sdiff command is showing two different text files side by side.

The | character shows that the lines are different. This is a different character that shows that there’s a new line. Instead, this one is indicating that the text on that line is different than the file it is being compared with.

See? Pretty simple stuff!

Closure:

Well, this is another way to compare two text files side by side in the Linux terminal. There are so many ways to accomplish a goal in Linux and this is just another way that you can do this operation on text files. This sort of stuff would be handy in dealing with revisions or easily spotting differences in readme files. You can apply this skill to all sorts of situations. Feel free to comment telling me how you use the sdiff command.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Last Updated on October 21, 2023 by KGIII

Subscribe to Newsletter!
Get notified when new articles are published!
We promise to never share your email!
icon

Author: KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.