image
Comparing Files
Start course
Difficulty
Beginner
Duration
7h 28m
Students
388
Ratings
3.5/5
Description

In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.

Transcript

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 line...to 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
Students
43997
Labs
168
Courses
1751
Learning Paths
45

A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.