In this video we’ll cover different ways of comparing files contents. 1
This is especially useful when you want to compare either the output of the same command at different 2
moments or the differences between a back up configuration file and the one currently used by the system. 3
Let's take a look at the first example. 4
I'm redirecting the output of the ifconfig command to two files at different moments. 5
So ifconfig > 6
so command redirection a 7
I'm generating some network traffic ping 8.8.8.8 I'm stopping it by pressing control 8
and c 9
And I'm redirecting the output of the same command to another file called b. 10
a and b were created in the current working directory; the files contain the output of the command, 11
but also the differences between the runs. 12
To see if there was any difference between the two runs of the command, I can compare the files contents. 13
If you only want to know if there is a difference run the cmp command: cmp a b 14
cmp compares the files byte by byte and displays the location of the first mismatch. It doesn't 15
see how the files differ 16
and if they are identical, no message is displayed. 17
A is equal to a. I'm copying the ls command to the current directory. 18
So, cp/usr /bin/ls . the current working directory. 19
This is the ls binary in the current directory and I want to see if it's identical to the original one. 20
cmp /bin/ls ./ls ls from the current working directory, it's 21
the same with simply writing ls. 22
It's the same. 23
And they're identical. 24
Nothing was displayed. 25
So note cmp can be used to compare binary files as well. 26
By the way, another way to compare 2 binary files to see if they are identical is to calculate and 27
compare their hash. 28
The hash is a unique cryptographic id for each file. 29
Let's calculate the hashes of the ls files using sha256: sha256sum 30
this is the name of the command and the path to the file. 31
So usr /bin/ls ./ls in the current working directory and they have the same hash, 32
meaning they're identical. 33
If even a single bit were different, the hashes would be different. 34
Now I’m changing the copied file. I’m appending something at the end, for example, echo ‘a’ > 35
ls 36
Let's see if the binaries are still identical: cmp/usr /bin/ls ls in the current working directory 37
and they are not identical anymore. 38
They differ on the first line; and the hashes? 39
The hashes aren't identical either. 40
Now, if you want to go deeper and see exactly how they differ, you should run the diff command 41
It's used only for text files. 42
diff a b 43
Diff stands for difference and is used to display the differences in the files by comparing 44
them line by line. 45
So from its output, it can be seen that the lines that differ are those related to the received and 46
transmitted packets. 47
This is normal because there was network traffic between the runs of the ifconfig command. 48
I've run the ping command. 49
Let's take another look of the output of the diff command. Let me explain to you how to interpret the output 50
of the diff command in another example. 51
First I’ll install the openssh server or daemon because we’ll need it anyway later in the 52
course. 53
So sudo apt istall ssh 54
And it's installing the open ssh server. 55
It's done, I'm copying the configuration file of the ssh demon in the current working directory 56
cp etc/ssh/sshd_config and the destination, the current directory 57
This is the configuration file of the demon that will be copied in the current directory. 58
I'm opening the file and changing the port option. 59
Of course I'm opening the copy. 60
And instead of Port 22, I'm writing Port 29. 61
And I'm saving the file. If you don't know how to use vi edit the file using the gedit, the graphical editor 62
available on Ubuntu. 63
This one! 64
Just go to etc ssh 65
and open the sshd_config file. 66
This is the file. 67
However, we'll go really deep into the vi editor in the next lectures. 68
And I'm running the diff command on the files,diff the original file ssh/sshd_config 69
and the file in my current working directory, the file I've modified. 70
Let's take a closer look at the output! It is showing us the line that differs and the line number. In this 71
example it is line15, the character c after 15 indicates that we have to change or replace the 72
line 15 in the first file by the line 15 in the second file to make them identical. 73
Two other meaningful characters are a and d where a means add or append in d means delete. 74
Lines preceded by a less than sign (<) are lines from the first file and the lines preceded by > 75
are lines from the second file. 76
In fact, the diff command helps us make the files identical and indicates exactly what should be done. 77
This is used by the patch command to patch a program that has a bug. 78
Let me tell you a few words about it. 79
Suppose a new bug which is a programming error was discovered. The solution to make things right is the 80
developer to change the source code . 81
Instead of releasing an entire program, the developer releases only a patch, which is in fact a file 82
that contains the differences between the file that has the bug and the file where the bug was solved. 83
This file and I mean the patch was generated by the deidd command after calculating the differences between 84
the two source files. 85
You can then apply the patch to the file that contains the bug using the patch tool. 86
The patch file indicates how the file should be changed so that it becomes identical to the other file, 87
the right one. Other useful options of the diff command are B to ignore blank lines; 88
W to ignore white spaces; 89
And I do ignore case differences in file contents. 90
And C for a more detailed comparison. 91
And the last option, which is Y if we want to display the entire file's contents on 2 column side 92
by side indicating the differences. 93
So diff -y and the files 94
Here's the difference. 95
Of course, a pipe to the less command would be much better.