Comparing Files

Start course
2h 10m

As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, we build upon some of the topics covered in our Linux Fundamentals course, including files and shell scripting, but also introduce new concepts such as wildcards, job control, switching users, and installing software. 

This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.

Learning Objectives

  • Learn what wildcards are and how and when to use them
  • Understand input, output, and redirection
  • Work with files and shell scripting
  • Implement processes and job control, and switch between users
  • Install software using RPM- and Debian-based systems

Intended Audience

  • Anyone with basic knowledge of Linux who wants to learn more
  • Professionals who want to learn more about Linux to enhance their career prospects


This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.


This lesson will cover how to compare the contents of files. If you want to compare two files and display the differences, you can use the diff command, the sdiff command, or vimdiff. The diff command will display the differences between two files, while the sdiff command will display the differences with file one on the left and file two on the right.

vimdiff will use the VIM editor to display the differences between two files. Here's just the first line of output produced by diff. The first number represents line numbers from the first file, and the second number represents line numbers from the second file. The middle character separating the line numbers will either be a "C" which represents a change, "D" which represents a deletion, or an "A" which represents an addition. 

In this example, the third line of the first file has changed or different than the third line in the second file. This is an example of an entire output from the diff command. The output that follows the "less than" sign belongs to the first file. The text following the "greater than" sign belongs to the second file. The three dashes are just a separator.

In the sdiff output the pipe or vertical bar character means that the text differs in the files on that line. You may also see the "less than" sign which means that the line only exists in the first file. The "greater than" sign means that line only exists in the second file. When you run vimdiff, both files will be pulled up in separate windows.

To switch between the windows, type "control w w". To close the current file in the current window, type "colon q enter". If you want to close both files at once and quit, type "colon q a", and if you've made some changes that you don't want to save, you can type "colon q a exclamation mark enter".

We'll use the "dash n" option for the cat command to display line numbers. Let's use the diff command to look at the differences between those two files.

You'll notice that the line that begins with a "less than" symbol belongs to the first file, while the line with the "greater than" symbol belongs to the second file. You'll also notice the first line of diff output says "four C four". That means the fourth line of the first file it's changed or different than the fourth line of the second file.

Let's use sdiff. And you can see that it places the files side by side and the vertical bar or the pipe symbol displays the line that has a difference. Let's add a new last this file. I'll append it, and we'll run sdiff again. And this time you can see that the "less than" sign represents that there is a line in the first file that is not in the second file. Use vimdiff.

You can see that VIM is highlighting the differences between the files. You can use "control w w" to move between windows. And if you want to close one window, "q", "colon q" to close the other window. Let's do that again and we'll close both of them at the same time. Can look at the differences and "colon q a enter" to exit vimdiff.

In this lesson we covered the diff command, which displays the differences between two files, the sdiff command which does the same thing except for it displays the differences side by side, and the vimdiff command which displays differences in the VIM editor.

About the Author
Jason Cannon
Founder, Linux Training Academy
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics