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.

Let’s Talk About Using dos2unix

Today, we’re going to talk about using dos2unix, a tool for converting Windows text files to Unix (Linux) formatted text files. The command isn’t very difficult to use, so this article is more about making you aware of the existence and purpose of said dos2unix. It should be a remarkably short article.

First, we have to go back in time…

Once upon a time, when you reached the end of the line with text, you had to instruct the hardware to start a new line. These were the old days of things like teletype machines and dumb terminals. They needed to be told when to begin a new line.

Well, that still exists in software today. The thing is, if a file was created on Windows it will have two characters denoting when a new line should begin while a file created on Linux will only have one character indicating when a new line should begin. Yay for compatibility issues!

Anyhow… This is why you will sometimes get errors with text files. For example, writing a shell script on Windows and then trying to use it on Linux may result in end of line errors. Windows uses CR (Carriage Return) and LF (Line Feed, you may know it as /n) while Linux (and Unix) just uses LF. This, of course, can cause some confusion for the operating system.

This is where dos2unix comes into play. This is why you’re getting an article about using dos2unix… So, with that in mind…

Using dos2unix:

This article requires an open terminal, like oh so many other articles. If you don’t know how to open the terminal, just press CTRL + ALT + T and your default terminal should open. Tada!

Now, you’ll need to install dos2unix. It probably isn’t installed by default. Just use your package manager’s standard install command and I’m like 99% certain it’s a package available to you by default. For example, in Debian, Ubuntu, and Ubuntu derivatives and flavors, it’s just a simple apt command:

Once you have dos2unix installed, you can convert Windows-created text files to Unix (and thus Linux) formatted text files by fixing the way they end a line and start a new line. That’s really easy. It’s just:

Yup…

Well, I suppose you could also do /path/to/filename.txt if you needed, or you can just navigate to the directory and use the command above. It really is that easy.

There’s more to the dos2unix application, but that’s about all you’re really going to need to know under normal circumstances. It really is that simple. Do check the man page (man dos2unix) for other options, but you’ll see that’s about all you’re gonna need.

Closure:

Like I said, this article is really about making you aware of the problem and solution, so that you can start using dos2unix if and when you need it. It turns out that dos2unix is more than just a contributing forum member on Linux.org, it’s actually a useful application for dealing with Windows files on a Linux box!

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 own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

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.